Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 구조화된 데이터
- react-hook-form
- useForm
- miniproject
- api 35
- Threppa
- html
- React
- react bits
- KBW
- mini
- 다중map
- React-native
- useEffect
- teamproject
- toy project
- next.js
- ToyProject
- JS
- css
- 팀프로젝트
- 문제
- Tanstack Query
- wescanner
- javascript
- 이중map
- 일본 우편번호 api
- echarts
- project
Archives
- Today
- Total
블로그 이름을 입력해주세요
[React] class동적으로 구현하기 본문
react에서 CSS의 class를 동적으로 주는 방법을 알아보자
예제)
App.js
function App() {
return (
<div className="App">
<div className='container'>
Color
</div>
</div>
);
}
App.css
.App {
text-align: center;
}
.container {
width: 500px;
height: 200px;
background-color: royalblue;
font-size: 50px;
color: #fff;
}
.red {
background-color: red;
}
동적으로 구현하려면 className에 조건을 주어야 한다
조건을 주려면 가장 간단한 방법은 빈 state주어 true와 false값으로 on off 방식을 주는 것
function App() {
const [onOff, setOnoff] = useState(false);
return (
<div className="App">
<div className='container'>
Color
</div>
</div>
);
}
버튼을 누르면 className을 조건 따라 부탁하면 된다
function App() {
const [onOff, setOnoff] = useState(false);
return (
<div className="App">
<div className={onOff === true ? "container red" : null}>
Color
</div>
<button onClick={() => {
setOnoff(true)
}}>버튼</button>
</div>
);
}

누르면

변경완료
'javaScript' 카테고리의 다른 글
[React] Styled-component 사용법 (이미지 넣는 법) (0) | 2022.07.05 |
---|---|
[React] usePrams() 사용법 (0) | 2022.06.30 |
[React] Component화 활용하기 (0) | 2022.06.26 |
[React] monster 컴포넌트 연습! (0) | 2022.06.21 |
[React] react 구조분해 할당? (0) | 2022.06.18 |