https://hyunu0504.github.io/blog9/
React App
hyunu0504.github.io
useEffect( () => { //실행할 코드 } )
useEffect( () => { //실행할 코드 } , [ value ] )
useEffect( () => { //실행할 코드 } , [ ] )
useEffect( () => {
//실행할 코드
return () => { //제거를 실행한 코드 }
} , [ ] })
https://hyunu0504.github.io/blog9/
App 컴포넌트
import "./App.css";
import styled from "styled-components";
import { useEffect, useState } from "react";
import Timer from "./timer";
const CountBox = styled.div`
background-color: yellow;
color: red;
padding-top: 30px;
padding-bottom: 30px;
`;
const Btn = styled.button`
padding: 30px;
`;
function App() {
const [count, setCount] = useState(true);
const [show, setShow] = useState(true);
useEffect(() => {
let f = setTimeout(() => {
setCount(false);
}, 3000);
}, []);
return (
<div className="App">
{count == true ? <CountBox>3초 후 사라지는 박스</CountBox> : null}
{true == show ? <Timer></Timer> : null}
<Btn onClick={() => setShow(!show)}>on/off 버튼</Btn>
</div>
);
}
export default App;
Timer 컴포넌트
import { useEffect, useState } from "react";
import styled from "styled-components";
const OnOffBox = styled.div`
background-color: red;
padding: 30px 0px 30px 0px;
font-size: 32px;
font-weight: bolder;
margin-bottom: 100px;
`;
function Timer() {
useEffect(() => {
const stop = setInterval(() => {
console.log("1s");
}, 1000);
return () => {
clearInterval(stop);
console.log("정지됨");
};
}, []);
return (
<div>
<OnOffBox>1초마다 콘솔창에 '1s'출력</OnOffBox>
</div>
);
}
export default Timer;
[React] 리액트 훅( Context API ) (0) | 2022.07.10 |
---|---|
[React] props (0) | 2022.07.09 |
[React] 리액트 훅( useState ) (0) | 2022.07.05 |
[React] CSS in JS ( styled-components ) (0) | 2022.07.01 |
[React] CSS ( module.css , sass/scss ) (0) | 2022.06.30 |