import { useEffect, useState } from "react";
import AppChild from "./AppChild";
const App = () => {
const [count, setCount] = useState(0);
const clickIncrease = () => {
setCount(count + 1);
};
console.log("App rendering");
useEffect(() => {
console.log("App useEffect ");
}, []);
const clickChild = () => {
console.log("clickChild");
};
return (
<div>
<button onClick={clickIncrease}></button>
<AppChild clickAppChild={clickChild}></AppChild>
</div>
);
};
export default App;
import { useEffect, useState } from "react";
const sum = (n) => {
console.log("sum calculate start");
let result = 0;
for (let i = 1; i <= n; i += 1) {
result += i;
}
console.log("sum calculateend");
return result;
};
const Child = ({ clickChild }) => {
const [sumNum, setSumNum] = useState(100000000);
const result = sum(sumNum);
console.log("Child rendering");
useEffect(() => {
console.log("Child useEffect ");
}, []);
return <div onClick={clickChild}>child , {result}</div>;
};
export default Child;
App์ปดํฌ๋ํธ๋ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ํ๊ฐ count๊ฐ ์ฆ๊ฐํ๋๊ฒ์ด ์๊ณ
AppChild์์ ๋ฐ์ ์ด๋ฒคํธ๋ฅผ ๋ถ๋ชจ ์ปดํฌ๋ํธ์ธ App์์ ๋์ํ๊ฒํ๋clickChild
๊ฐ ์๋ค.
AppChild์ปดํฌ๋ํธ์์๋ click ์ด๋ฒคํธ๊ฐ ์๊ณ ์ด๋ฅผ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ก ์ฌ๋ ค์ฃผ๋ clickChild๊ฐ ์๋ค.
๊ทธ๋ฆฌ๊ณ sumํจ์๋ฅผ ํตํด result๊ฐ ์กด์ฌํ๋ค.
๊ธฐ๋ณธ์ ์ธ ์ด๊ธฐ ์ฝ์ ๊ฒฐ๊ณผ์ด๋ค.
App ์ฝ์์ถ๋ ฅ -> return์ AppChild์ ๋ง๋ AppChild ์ปดํฌ๋ํธ ์ฝ๋์คํ -> AppChild ์ปดํฌ๋ํธ์ sum ์คํ -> AppChild ์ฝ์์ถ๋ ฅ -> AppChild๊ฐ ๋ ๋๋ง์ด๋๋ฉด useEffect ์คํ ๋ฐ ์ฝ์ ์ถ๊ฐ -> App์ด ๋ ๋๋ง์ด๋๋ฉด useEffect ์คํ ๋ฐ ์ฝ์ ์ถ๊ฐ
๋ฒํผ์ ๋๋ฅด๊ฒ ๋๋ค๋ฉด(2๋ฒ์คํ) App๊ณผ Child ๋ชจ๋ ๋ฆฌ๋ ๋๋ง๋๋ค.
์ฐ๋ฆฌ๋ count๊ฐ์ Child์ ์ฐ๊ด์ด ์๋๋ฐ๋ ๋จ์ง ๋ถ๋ชจ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ค๋ ์ด์ ๋ก ๊ฐ์ด ๋ ๋๋ง๋๋ค. ์ด๋ก์ธํด sum ํจ์ ๋ํ ์คํ๋๋ค.
sum ํจ์๋ ๋จ์ง ์ํฅ์ ๋ฐ๋
sumNum
์ ์ํด์๋ง ์คํ๋๊ฒ ํ๊ณ ์ถ๋ค.
ํด๋น ์ฝ๋๋ฅผ ์์ ํด๋ณธ๋ค.
const result = useMemo(() => sum(sumNum), [sumNum]);
2๋ฒ ํด๋ฆญ์ ๋๋ ์ ๋ AppChild์ sumํจ์๋
sumNum
์ ๊ฐ์ด ๋ฐ๋์ง์๋ ์ด์ ํ์ง์์ ๋ถํ์ํ sumํจ์์ ์คํ๊ณผ ๊ด๋ จ๋ ๋ ๋๋ง์ด ๋ฐ์ํ์ง ์๋๋ค.
์ ์ ์ฝ๋์์ ์ด๋ฒคํธ๋ฅผ props๋ก ์ฃผ๋ ์ฝ๋๋ฅผ ์๋์๊ฐ์ด ์๋ตํด๋ณด์
import { useEffect, useState } from "react";
import AppChild from "./AppChild";
const App = () => {
const [count, setCount] = useState(0);
const clickIncrease = () => {
setCount(count + 1);
};
console.log("App rendering");
useEffect(() => {
console.log("App useEffect ");
}, []);
// const clickChild = () => {
// console.log("clickChild");
// };
return (
<div>
<button onClick={clickIncrease}></button>
<AppChild></AppChild>
</div>
);
};
export default App;
import { useEffect, useState, useMemo } from "react";
const sum = (n) => {
console.log("sum calculate start");
let result = 0;
for (let i = 1; i <= n; i += 1) {
result += i;
}
console.log("sum calculateend");
return result;
};
const Child = () => {
const [sumNum, setSumNum] = useState(100000000);
const result = useMemo(() => sum(sumNum), [sumNum]);
console.log("Child rendering");
useEffect(() => {
console.log("Child useEffect ");
}, []);
return <div>child , {result}</div>;
};
export default Child;
ํ์ฌ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฒํผ์ ๋๋ฌ App์ count ์ํ๊ฐ์ด ๋ฐ๋๋ค๋ฉด AppChild๊ฐ ๋ฆฌ๋ ๋๋ง์ด ๋์ด
console.log('Child rendering')
์ด ์คํ๋๋ค.
memo๋ก Child ์ปดํฌ๋ํธ๋ฅผ ์ ์ฒด๋ฅผ ๋ฌถ์ด์ค๋ค.
์ด๋ฅผ ํตํด ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋๋ผ๋ ์์ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ ๊ฒ์ ๋ง์์ค๋ค.
import { useEffect, useState, useMemo, memo } from "react";
const sum = (n) => {
console.log("sum calculate start");
let result = 0;
for (let i = 1; i <= n; i += 1) {
result += i;
}
console.log("sum calculateend");
return result;
};
const Child = memo(() => {
const [sumNum, setSumNum] = useState(100000000);
const result = useMemo(() => sum(sumNum), [sumNum]);
console.log("Child rendering");
useEffect(() => {
console.log("Child useEffect ");
}, []);
return <div>child , {result}</div>;
});
export default Child;
์ด์๊ฐ์ด ๋ฒํผ์ ๋๋ฌ count๊ฐ ๋ฐ๋์ด๋ AppChild๊ฐ ๋ฆฌ๋ ๋๋ง๋์ง์๋๋ค.
๋ค์ ์ฒ์์ App.js์ AppChild.jsx์์ memo์ useMemo๋ฅผ ์ถ๊ฐํ์ฌ ๋์๊ฐ๋ณด์
import { useEffect, useState } from "react";
import AppChild from "./AppChild";
const App = () => {
const [count, setCount] = useState(0);
const clickIncrease = () => {
setCount(count + 1);
};
console.log("App rendering");
useEffect(() => {
console.log("App useEffect ");
}, []);
const clickChild = () => {
console.log("clickChild");
};
return (
<div>
<button onClick={clickIncrease}></button>
<AppChild clickChild={clickChild}></AppChild>
</div>
);
};
export default App;
import { useEffect, useState, useMemo, memo } from "react";
const sum = (n) => {
console.log("sum calculate start");
let result = 0;
for (let i = 1; i <= n; i += 1) {
result += i;
}
console.log("sum calculateend");
return result;
};
const Child = memo(({ clickChild }) => {
const [sumNum, setSumNum] = useState(100000000);
const result = useMemo(() => sum(sumNum), [sumNum]);
console.log("Child rendering");
useEffect(() => {
console.log("Child useEffect ");
}, []);
return <div onClick={clickChild}>child , {result}</div>;
});
export default Child;
ํด๋น ์ฝ๋์์๋ ๋ฒํผ์ ๋๋ฌ(2๋ฒํด๋ฆญ) count ์ํ๊ฐ์ด ๋ฐ๋๋ฉด AppChild๊ฐ ๋ฆฌ๋ ๋๋ง๋๋ค.
์ด๋ ๋งค๊ฐ๋ณ์๋ก ์จ clickChild๊ฐ App ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง๋๋ฉด์ ์ด์ ๊ณผ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ธ์๋๊ธฐ ๋๋ฌธ์ด๋ค.
const clickChild = useCallback(() => {
console.log("clickChild");
}, []);
๊ธฐ์กด ํจ์์์ ์ ์ฒด๋ฅผ useCallback์ผ๋ก ๋ฌถ์ด์ ์ฒ๋ฆฌํ๋ค.
์ด๋ฅผ ํตํด App์ด ๋ ๋๋ง๋์ด๋ clickChild๊ฐ ์ฌ์ ์๋๋ฉด์ ์๋ก์ด ์ ์๊ฐ ๋ ํ์์๊ณ (์ฃผ์๊ฐ์ด ๋ฐ๋ํ์๋์๊ณ ) ์ด๋ฅผ ๋ฐ๋๋ ๊ฒ์ผ๋ก ์ธ์ํ์ง์์์ AppChild๊ฐ ๋ฆฌ๋ ๋๋ง๋ ํ์๋ ์๋ค.
import { useEffect, useState, useCallback } from "react";
import AppChild from "./AppChild";
const App = () => {
const [count, setCount] = useState(0);
const clickIncrease = () => {
setCount(count + 1);
};
console.log("App rendering");
useEffect(() => {
console.log("App useEffect ");
}, []);
const clickChild = useCallback(() => {
console.log("clickChild");
}, []);
return (
<div>
<button onClick={clickIncrease}></button>
<AppChild clickChild={clickChild}></AppChild>
</div>
);
};
export default App;
import { useEffect, useState, useMemo, memo } from "react";
const sum = (n) => {
console.log("sum calculate start");
let result = 0;
for (let i = 1; i <= n; i += 1) {
result += i;
}
console.log("sum calculateend");
return result;
};
const Child = memo(({ clickChild }) => {
const [sumNum, setSumNum] = useState(100000000);
const result = useMemo(() => sum(sumNum), [sumNum]);
console.log("Child rendering");
useEffect(() => {
console.log("Child useEffect ");
}, []);
return <div onClick={clickChild}>child , {result}</div>;
});
export default Child;
memo , useMemo ,useCallback์ ๊ฐ๊ฐ์ ํ์ํ ์ํฉ์ด ์กด์ฌํ๋ค.
memo๋ ๋ถ๋ชจ๊ฐ ๋จ์ง ๋ฆฌ๋ ๋๋ง๋๋ค๊ณ ( ๋ถ๋ชจ๋ก๋ถํฐ ๋ฐ์ props๊ฐ ๋ฐ๋๊ฒ๋ ์๋๋ฐ) ์์์ด ๋ฆฌ๋ ๋๋ง ๋๋ ๊ฒ์ ๋ง๊ณ
useMemo๋ ๋ฆฌ๋ ๋๋ง์ด ๋๋ ์ปดํฌ๋ํธ ๋ด๋ถ์์ ์๊ฐ์ด ๋ง์ด๋๋ ํจ์๊ณ์ฐ์ ํ์ฌ ๊ฐ์ ๋ฐํํ๋ ๊ฒ์ ๋ถํ์ํ ์คํ์ ๋ง๊ธฐ ์ํด ์ฌ์ฉํ๊ณ
useCallback์ ๋ฆฌ๋ ๋๋ง์ ์ํด ๋ถํ์ํ ์ฌ์ ์๋ฅผ ํตํด props๋ก ์ ๋ฌ๋์ด ์ํ๋ณํ๋ก ์ธ์๋์ด ์์์์ ๋ถํ์ํ ๋ฆฌ๋ ๋๋ง์ ์คํํ๊ฒ ๋ง๋๋ ๊ฒ์ ๋ง์์ค๋ค. (memo๋ก๋ ๋ถ๋ชจ๋ก๋ถํฐ ๋ฐ์ props๊ฐ ๋ณํ๋ค๋ฉด ๋ง์ ์ค ์๊ฐ ์๊ธฐ ๋๋ฌธ)