const Test = () => {
const [count, setCount] = useState(1)
useEffect(() => {
setCount(count + 1);
setCount((count) => count + 1)
setCount(count + 1);
setCount((count) => count + 1)
setCount(count + 1);
setCount((count) => count + 1)
}, [])
const [count1, setCount1] = useState(1);
useEffect(()=>{
setCount1(count1 + 1);
setCount1(count1 + 1);
setCount1(count1 + 1);
},[])
const [count2, setCount2] = useState(1);
useEffect(()=>{
setCount2(prev=> prev + 1);
setCount2(prev=> prev + 1);
setCount2(prev=> prev + 1);
}, [])
return (
<div className='headerStyle'>
count: {count}
<br/>
count1: {count1}
<br/>
count2: {count2}
</div>
)
}
setCount(count + 1);
count는 1을 참조한다setCount((count) => count + 1)
prev는 2를 참조한다//count가 바라보고 있는 값 => 1
setCount( 1 + 1 )
setCount ( 1 + 1 )
setCount ( 1 + 1 )
setCount ((prev) => prev + 1 )
동기적으로 처리하는 방법이다setCount( 1 => 1 + 1 )
setCount( 2 => 2 + 1 )
setCount( 3 => 3 + 1 )
출처 : https://www.philly.im/blog/things-to-know-about-use-state
https://velog.io/@alstnsrl98/useState%EB%8A%94-%EB%8F%99%EA%B8%B0-%EB%B9%84%EB%8F%99%EA%B8%B0-%EB%8F%99%EA%B8%B0%EC%A0%81-%EC%B2%98%EB%A6%AC
https://taenami.tistory.com/49