๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

react2

React) useEffect์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž React๋ฅผ ๋‹ค์‹œ ๋ณต์Šต ์ค‘์— ํ•œ๋ฒˆ ์ •๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ๊ธ€์„ ์จ ๋ณด๋ ค ํ•œ๋‹ค. const App = () => { const [count, setCount] = useState(0); console.log("rendering"); const add = () => { console.log("add"); setCount(current => { return current+1 }); }; const minus = () => { console.log("minus"); setCount(current => { return current-1 }); }; useEffect(() => { console.log("component render"); return () => { console.log("component end.. 2021. 10. 6.
React) ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง์„ ์—†์• ๋ณด์ž! ๋ฆฌ์•กํŠธ์—์„œ setState๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งˆ๋‹ค ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚œ๋‹ค. ๊ณผ๋„ํ•œ ๋ Œ๋”๋ง์€ ํŽ˜์ด์ง€ ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง์„ ์ตœ์†Œํ™” ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. Class๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ 1. shouldComponentUpdate() shouldComponentUpdate(nextProps, nextState, nextContext){ if(this.state.counter !== nextState.counter){ return true; } return false; } shouldComponentUpdate()๋ฅผ ์ด์šฉํ•ด ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” state์˜ counter ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ์—๋งŒ ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค. 2. PureComponent .. 2021. 6. 28.
๋ฐ˜์‘ํ˜•