๋ฆฌ์กํธ๋ state๊ฐ ๋ณ๊ฒฝ
๋๋ฉด ํด๋นํ๋ ๋ชจ๋ ์ปดํฌ๋ํธ
๋ฅผ ์ฌ๋ ๋๋ง
ํ๋ค.
์ด๋ ์ฌ๋๋๋งํด์ผํ๋ ์ปดํฌ๋ํธ๊ฐ ๋ง์์๋ก
์ฌ์ดํธ๋ ๋ฒ๋ฒ
์ผ ๊ฒ์ด๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด memo()
๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
<div>์์
๋ฅผ ๋ ๋๋งํ๊ธฐ์ ์ console.log์ ํด๋นํ๋ ๊ฐ์ ์ถ๋ ฅํ๋ค.์ฒ์ ๋ ๋๋ง๋ console
function App() {
const [value, setValue] = useState('Save changes');
const changeValue = () => setValue('Revert Changes');
return (
<div className="App">
<MemoBtn innerText={value} changeValue={changeValue} />
<MemoBtn innerText="domado" />
</div>
);
}
function Btn({ innerText, changeValue }) {
console.log(innerText, 'rendering');
return (
<button
type="button"
style={{
backgroundColor: 'tomato',
color: 'white',
padding: '10px 20px',
border: 0,
borderRadius: 10,
}}
onClick={changeValue}
>
{innerText}
</button>
);
}
const MemoBtn = React.memo(Btn);
๋ฒํผ์ ๋๋ฅธ ํ ๋ ๋๋ง๋ console