I'm trying to make toggle menu by using state. I want to hide component when the value of state is false. So I made conditional rendering in styled-component but there is an error. It says "'isChecked' is not defined no-undef". I want to fix this error but don't know how to fix it and where is wrong. I'd appreciate it if you let me know Thanks!
This is my code. The cord was long, so I erased the irrelevant part. It's still a long code, but I think you just need to look at the part I commented on
import React, { useState } from 'react'
import styled from 'styled-components';
const Body3LeftStart = styled.div`
flex-basis: 66.66666667% !important;
max-width: 66.66666667%;
box-sizing: border-box;
flex: 0 0 auto;
padding-left: 8px;
padding-right: 8px;
`
const Body3LeftSection = styled.div`
margin-bottom: 8px;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #000a12;
box-sizing: border-box;
#Body3Section1 {
display: flex;
align-items: flex-end;
margin-bottom: 8px;
}
#Body3Section1::before {
display: block;
margin-top: -71px;
padding-top: 71px;
visibility: hidden;
content: " ";
z-index: 0;
}
#Body3Section1Span {
margin-right: 8px;
line-height: 1.45;
letter-spacing: -.3px;
color: #343a40;
font-size: 22px;
font-weight: 700;
}
#Body3Section1Span2 {
line-height: 1.5;
letter-spacing: -.3px;
font-size: 16px;
color: #adb5bd;
font-weight: 500;
}
`
const Body3LeftSection2 = styled.div`
display: flex;
margin-bottom: 20px;
align-items: flex-end;
#Body3LeftSection2Text {
font-weight: 400;
line-height: 1.47;
letter-spacing: -.3px;
font-size: 15px;
margin-right: 12px;
color: #495057;
word-break: keep-all;
}
`
const Body3LeftToggle = styled.div`
overflow: hidden;
border: 1px solid #e9ecef;
border-radius: 4px;
`
const Body3LeftToggle1 = styled.div`
box-sizing: border-box;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
`
const Body3LeftToggleTitle = styled.div`
border-top: unset;
display: flex;
align-items: center;
padding: 15px 20px;;
border-bottom: 1px solid #f1f3f5;
background-color: #f8f9fa;
cursor: pointer;
#Body3LeftToggleButton {
display: inline-flex;
margin-right: 8px;
width: 16px;
height: 16px;
align-items: center;
justify-content: center;
transition: background-color .15s ease;
}
`
const Body3LeftToggleTitleText = styled.div`
line-height: 1.47;
letter-spacing: -.3px;
font-size: 15px;
color: #343a40;
font-weight: 700;
font-style: inherit;
`
const Body3LeftToggleTitleText2 = styled.div`
font-weight: 400;
line-height: 1.47;
letter-spacing: -.3px;
font-size: 15px;
margin-left: auto;
color: #343a40;
`
//This is the component I want to put in a toggle. I used conditional rendering here.
const Body3LeftToggleContent = styled.div`
max-height: 50px;
overflow: hidden;
${isChecked===false}{
display: none;
}
`
const Body3LeftToggleContentWrap = styled.div`
display: flex;
align-items: center;
padding: 14px 20px;
`
function Body3Left() {
// I made useState here
const [isChecked,setChecked] = useState(false)
return (
<Body3LeftStart>
<Body3LeftSection>
<div id='Body3Section1'>
<span id='Body3Section1Span'>
커리큘럼
</span>
<span id='Body3Section1Span2'>
총 39
개 ˙ 5시간 2분의 수업
</span>
</div>
<Body3LeftSection2>
<span id='Body3LeftSection2Text'>
이 강의는 영상, 수업 노트가 제공됩니다. 미리보기를 통해 콘텐츠를 확인해보세요.
</span>
<button id='Body3LeftSection2Button'>
모두 접기
</button>
</Body3LeftSection2>
<Body3LeftToggle>
<Body3LeftToggle1>
<Body3LeftToggleTitle>
// I used button here. I erased it because the svg code was long, but there is an arrow picture in it. I changed the value every time I pressed the button
<span id='Body3LeftToggleButton'>
<svg onClick={()=>{setChecked(!isChecked)}} style={{display:isChecked===true&&'none' ,width:'16', height:'16'}}></svg>
<svg onClick={()=>{setChecked(!isChecked)}} style={{display:isChecked===false&&'none' ,width:'16', height:'16'}}></svg>
</span>
<Body3LeftToggleTitleText>
섹션 0. 소개
</Body3LeftToggleTitleText>
<Body3LeftToggleTitleText2>
1강 ∙ 4분
</Body3LeftToggleTitleText2>
</Body3LeftToggleTitle>
<Body3LeftToggleContent>
<Body3LeftToggleContentWrap>
<span id='Body3LeftToggleContentText1'>
강의 소개
</span>
<span id='Body3LeftToggleContentText2'>
<span id='Body3LeftToggleContentText3'>
04:41
</span>
</span>
</Body3LeftToggleContentWrap>
</Body3LeftToggleContent>
</Body3LeftToggle1>
</Body3LeftToggle>
</Body3LeftSection>
</Body3LeftStart>
)
}
export default Body3Left;
you have to pass the state as props to the styled component as
const Body3LeftToggleContent = styled.div`
max-height: 50px;
overflow: hidden;
display: ${props => props.isChecked ? "block" : "none"}; // get prop value as so
`;
from the JSX as
</Body3LeftToggleTitle>
<Body3LeftToggleContent isChecked={isChecked}> // like so
<Body3LeftToggleContentWrap>
only added the parts of the code to alter ....
for reference on passing props
how can i move image a very little bit up and add text below it { note i am talking about the highlighted profile image with blue } [image here][1] also see my code and tell every step to do that i have tired margin button but it didn't work
`import { Header } from "../../features/theme/Header";
import styles from "./Home.module.css";
import { useAppSelector } from "../../app/hooks";
import { Feed } from "../../features";
export function Home(): JSX.Element {
const { currentUserImage } = useAppSelector((state) => state.currentUser);
return (
<div className={styles.home}>
<Header page="Home" />
<div className={styles.tweetField}>
<div className={styles.userAvatar}>
<img src={currentUserImage} alt="" />
</div>
<div>
<div>
</div>
</div>
</div>
<Feed />
</div>
);
}
my css code
.home{
border-right: solid 0.2px var(--border-color);
}
.homeHeader{
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
border-bottom: solid 0.2px var(--border-color);
font-size: 1.2rem;
font-weight: bold;
}
.homeHeader button{
border: none;
background: none;
font-size: 1.3rem;
color: var(--theme-page-text);
}
.tweetField{
display: flex;
margin-top: 1rem;
border-bottom: solid 0.2px var(--border-color);
}
.userAvatar{
outline: none;
width: 3rem;
height: 3rem;
border-radius: 50%;
margin-left: 1rem;
background: #fff;
}
.userAvatar img{
width: 3rem;
height: 3rem;
border-radius: 50%;
object-fit: cover;
background: #fff;
}
.tweetActions{
width: 100%;
margin: 1rem;
}
.tweetActions span{
font-size: 1.3rem;
color: var(--light-text-color);
}
.tweetActions div{
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 1rem;
}
.tweetActions button:first-child{
border: none;
background: none;
outline: none;
color: var(--primary-color);
font-size: 1.2rem;
}
.tweetActions button:last-child{
border: none;
outline: none;
color: #fff;
background: var(--primary-color);
opacity: 0.5;
font-size: 1rem;
font-weight: bold;
padding: 0.5rem 1.2rem;
border-radius: 2rem;
}
image here =
[1]: https://i.stack.imgur.com/JpZKd.png
how can i move image a very little bit up and add text below it { note i am talking about the highlighted profile image with blue }also see my code and tell every step to do that i have tired margin button but it didn't work pls tell how to do pls also see the image image here
`import { Header } from "../../features/theme/Header";
import styles from "./Home.module.css";
import { useAppSelector } from "../../app/hooks";
import { Feed } from "../../features";
export function Home(): JSX.Element {
const { currentUserImage } = useAppSelector((state) => state.currentUser);
return (
<div className={styles.home}>
<Header page="Home" />
<div className={styles.tweetField}>
<div className={styles.userAvatar}>
<img src={currentUserImage} alt="" />
</div>
<div>
<div>
</div>
</div>
</div>
<Feed />
</div>
);
}
my css code
.home{
border-right: solid 0.2px var(--border-color);
}
.homeHeader{
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
border-bottom: solid 0.2px var(--border-color);
font-size: 1.2rem;
font-weight: bold;
}
.homeHeader button{
border: none;
background: none;
font-size: 1.3rem;
color: var(--theme-page-text);
}
.tweetField{
display: flex;
margin-top: 1rem;
border-bottom: solid 0.2px var(--border-color);
}
.userAvatar{
outline: none;
width: 3rem;
height: 3rem;
border-radius: 50%;
margin-left: 1rem;
background: #fff;
}
.userAvatar img{
width: 3rem;
height: 3rem;
border-radius: 50%;
object-fit: cover;
background: #fff;
}
.tweetActions{
width: 100%;
margin: 1rem;
}
.tweetActions span{
font-size: 1.3rem;
color: var(--light-text-color);
}
.tweetActions div{
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 1rem;
}
.tweetActions button:first-child{
border: none;
background: none;
outline: none;
color: var(--primary-color);
font-size: 1.2rem;
}
.tweetActions button:last-child{
border: none;
outline: none;
color: #fff;
background: var(--primary-color);
opacity: 0.5;
font-size: 1rem;
font-weight: bold;
padding: 0.5rem 1.2rem;
border-radius: 2rem;
}
wewaewavresrvesgrefesfvesfvesfrervesrservesrves
There's a weird bug in my application to causes some CSS properties to disappear. When I sign-in into my application, I used <Redirect to="/home"/> to go to my home page. And then some images disappear and some button are half the size they suppose to be. When I refresh the browser, page gets back to what it supposes to be. I'm using styled component mainly, and below are some pictures:
after signin, wrong version
after refresh page, correct version
here is the complete codehttps://github.com/aojiaooo/shopaholic-react-app
To run the code, you have to change SignIn.js function below:
async function loginUser(credentials) {
return "someRandomCookieOfYourChoice";
//below was the original request to backend
/**
return await axios.post('http://localhost:8080/signin', credentials)
.then(response => response.data.code === 0 ? response.data.data.value : null);
*/
}
Have being frustrated the whole day ... Any help would be greatly appreciated. I'm a backend guy and this is my first day trying out React. Thanks in advance!
import styled from "styled-components";
import { Link } from "react-router-dom";
//general layout for shopaholic
export const SectionWrapper = styled.section`
box-shadow: 0 0 5px #ccc;
padding: 15px 10px;
max-width: 1200px;
margin: 0 auto;
box-sizing: border-box;
-moz-osx-font-smoothing: grayscale;
`
// style for header
export const HeaderWrapper = styled.header`
min-height: 70px;
display: flex;
justify-content: space-around;
align-items: center;
overflow: hidden;
.menu, .close{
cursor: pointer;
display: none;
}
.hello {
display: inline-block;
padding: 10px 10px 15px 15px;
height: 20px;
margin-left: -50px;
}
#media (max-width: 1000px){
ul{
position: fixed;
top:0;
left: -100%;
width: 100%;
height: 100vh;
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: column;
background: white;
z-index: 99;
opacity: 0.97;
transition: 0.5s linear;
margin-left: -20px;
}
.menu, .close{
display: block;
}
.close{
position: relative;
}
ul.toggle{
left: 0;
}
}
`
export const HeaderLogoLink = styled(Link)`
font-size: 2em;
font-weight: bold;
text-decoration: none;
text-transform: uppercase;
color: black;
display: block;
padding-left: 30px;
`
export const HeaderNavWrapper = styled.nav`
display: flex;
padding-right: -100px;
ul li a:hover{
color: darkgray;
}
ul li a{
text-decoration: none;
text-transform: uppercase;
color: #555;
padding: 0 15px;
}
ul li{
list-style: none;
display: inline-block;
}
`
// style for home page
export const ProductListWrapper = styled.div`
width: 100%;
display: flex;
justify-content: space-evenly;
align-content: flex-start;
flex-wrap: wrap;
`
export const CardWrapper = styled.div`
min-width: 300px;
min-height: 400px;
border: 1px solid #eee;
overflow: hidden;
padding: 10px;
box-shadow: 2px 8px 20px #ddd;
margin: 10px 15px;
transition: 0.5s linear;
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
display: block;
`
export const CardInfoWrapper = styled.div`
margin: 10px 15px;
text-decoration: none;
a{
text-decoration: none;
color: #333;
}
a:hover{
color: darkblue;
}
`
export const CardImageWrapper = styled.img`
max-width: 300px;
max-height: 300px;
display: inline-block;
color: white;
text-decoration: none;
padding: 10px 15px;
`
export const CardTextWrapper = styled.div`
padding: 0;
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
line-height: 1.5;
margin: -5px 0;
-webkit-font-smoothing: antialiased;
`
export const CardTitleLinkWrapper = styled(Link)`
font-size: 1.5em;
font-weight: bold;
margin-bottom: 10px;
margin-top: -5px;
font-family: 'Segoe UI',serif;
display: inline-block;
-webkit-font-smoothing: antialiased;
`
export const CardDetailLinkWrapper = styled(Link)`
background: #333;
text-align: center;
width: 300px;
margin-left: 10px;
height: 25px;
padding-top: 15px;
padding-bottom: 10px;
font-size: 1em;
color: white;
text-decoration: none;
display: inline-block;
-webkit-font-smoothing: antialiased;
`
// style for detail page
export const ProductDetailWrapper = styled.div`
display: flex;
justify-content: space-around;
flex-wrap: wrap;
margin: 50px 0;
box-sizing: border-box;
`
export const DetailImageWrapper = styled.img`
max-width: 500px;
min-width: 290px;
height: 300px;
display: block;
object-fit: cover;
margin: 25px;
`
export const DetailInfoWrapper = styled.div`
max-width: 500px;
min-width: 290px;
margin: 25px;
`
export const DetailTitleWrapper = styled.div`
display: block;
font-size: 1.5em;
margin-block-start: 0.83em;
margin-block-end: 0.83em;
font-weight: bold;
box-sizing: border-box;
font-family: 'Segoe UI',serif;
letter-spacing: 1px;
`
export const DetailButtonWrapper = styled.button`
background: #333;
color: white;
text-decoration: none;
padding: 10px 35px;
margin-top: 30px;
display: inline-block;
font-family: 'Georgia', sans-serif;
`
export const DetailTextWrapper = styled.div`
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
-webkit-font-smoothing: antialiased;
line-height: 1.5;
margin: 10px 0;
`
// style for alert box
export const AlertTextWrapper = styled.div`
font-family: '.AppleSystemUIFont', sans-serif;
-webkit-font-smoothing: antialiased;
`
I am not sure, but you may try this...
import styled from 'styled-components';
import { Link } from 'react-router-dom';
// general layout for shopaholic
export const SectionWrapper = styled.section`
box-shadow: 0 0 5px #ccc;
padding: 15px 10px;
max-width: 1200px;
margin: 0 auto;
box-sizing: border-box;
-moz-osx-font-smoothing: grayscale;
`;
// style for header
export const HeaderWrapper = styled.header`
min-height: 70px;
display: flex;
justify-content: space-around;
align-items: center;
overflow: hidden;
.menu,
.close {
cursor: pointer;
display: none;
}
.hello {
display: inline-block;
padding: 10px 10px 15px 15px;
height: 20px;
margin-left: -50px;
}
#media (max-width: 1000px) {
ul {
position: fixed;
top: 0;
left: -100%;
width: 100%;
height: 100vh;
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: column;
background: white;
z-index: 99;
opacity: 0.97;
transition: 0.5s linear;
margin-left: -20px;
}
.menu,
.close {
display: block;
}
.close {
position: relative;
}
ul.toggle {
left: 0;
}
}
`;
export const HeaderLogoLink = styled(Link)`
font-size: 2em;
font-weight: bold;
text-decoration: none;
text-transform: uppercase;
color: black;
display: block;
padding-left: 30px;
`;
export const HeaderNavWrapper = styled.nav`
display: flex;
padding-right: -100px;
ul li a:hover {
color: darkgray;
}
ul li a {
text-decoration: none;
text-transform: uppercase;
color: #555;
padding: 0 15px;
}
ul li {
list-style: none;
display: inline-block;
}
`;
// style for home page
export const ProductListWrapper = styled.div`
width: 100%;
display: flex;
justify-content: space-evenly;
align-content: flex-start;
flex-wrap: wrap;
`;
export const CardWrapper = styled.div`
min-width: 300px;
min-height: 400px;
border: 1px solid #eee;
overflow: hidden;
padding: 10px;
box-shadow: 2px 8px 20px #ddd;
margin: 10px 15px;
transition: 0.5s linear;
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
display: block;
`;
export const CardInfoWrapper = styled.div`
margin: 10px 15px;
text-decoration: none;
a {
text-decoration: none;
color: #333;
}
a:hover {
color: darkblue;
}
`;
export const CardImageWrapper = styled.img`
max-width: 300px;
max-height: 300px;
display: inline-block;
color: white;
text-decoration: none;
padding: 10px 15px;
`;
export const CardTextWrapper = styled.div`
padding: 0;
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
line-height: 1.5;
margin: -5px 0;
-webkit-font-smoothing: antialiased;
`;
export const CardTitleLinkWrapper = styled(Link)`
font-size: 1.5em;
font-weight: bold;
margin-bottom: 10px;
margin-top: -5px;
font-family: 'Segoe UI', serif;
display: inline-block;
-webkit-font-smoothing: antialiased;
`;
export const CardDetailLinkWrapper = styled(Link)`
background: #333;
text-align: center;
width: 300px;
margin-left: 10px;
height: 25px;
padding-top: 15px;
padding-bottom: 10px;
font-size: 1em;
color: white;
text-decoration: none;
display: inline-block;
-webkit-font-smoothing: antialiased;
`;
// style for detail page
export const ProductDetailWrapper = styled.div`
display: flex;
justify-content: space-around;
flex-wrap: wrap;
margin: 50px 0;
box-sizing: border-box;
`;
export const DetailImageWrapper = styled.img`
max-width: 500px;
min-width: 290px;
height: 300px;
display: block;
object-fit: cover;
margin: 25px;
`;
export const DetailInfoWrapper = styled.div`
max-width: 500px;
min-width: 290px;
margin: 25px;
`;
export const DetailTitleWrapper = styled.div`
display: block;
font-size: 1.5em;
margin-block-start: 0.83em;
margin-block-end: 0.83em;
font-weight: bold;
box-sizing: border-box;
font-family: 'Segoe UI', serif;
letter-spacing: 1px;
`;
export const DetailButtonWrapper = styled.button`
background: #333;
color: white;
text-decoration: none;
padding: 10px 35px;
margin-top: 30px;
display: inline-block;
font-family: 'Georgia', sans-serif;
`;
export const DetailTextWrapper = styled.div`
box-sizing: border-box;
font-family: 'Georgia', sans-serif;
-webkit-font-smoothing: antialiased;
line-height: 1.5;
margin: 10px 0;
`;
// style for alert box
export const AlertTextWrapper = styled.div`
font-family: '.AppleSystemUIFont', sans-serif;
-webkit-font-smoothing: antialiased;
`;
Im not sure if this work with styled component!! The use of the !important on the style that is being ignored, or overridden in react. In normal css i will do it this way:
.selector{
background-color: red !important;
}
You can try this:
export const DetailButtonWrapper = styled.button`
background: #333 !important ;
`;
Im not certain about this answer but yiu can try it. !important on the style which are not applying
It turns out this problem happens when running the app using
npm start
however, the css issue goes away when doing npm run build and then serve -s build
I guess production wise this is not a bug but rather something wrong with the development environment, in case any one in the future has this problem, one can try build the project and see if the error goes away.
I wanted to put some space between scrollbar and edge of textarea and have this code:
export const Textarea = styled.textarea`
font-family: Open Sans, sans-serif;
font-size: 16px;
line-height: 24px;
border-radius: 14px;
width: 600px;
height: 200px;
border-width: 0px;
border: 1px solid #DCDCDC;
padding: 14px 16px;
left:-100px;
resize: none;
box-sizing: border-box;
::-webkit-scrollbar {
width: 8px;
height: 60px;
}
::-webkit-scrollbar-thumb {
background-color: #3AA4A4;
border-radius:2px;
border-right: 4px white solid;
background-clip: padding-box;
}
::-webkit-scrollbar-track {
margin-top: 20px;
margin-bottom: 20px;
}
:focus {
outline: none;
}
:placeholder {
color: #96a2ac;
}
`;
but the problem is the space was added but border radius of scrollbar of top right and bottom right is not added and visible.