2024-03-03.md
๐กDIL: ๋ชจ๋ ๋ฆฌ์กํธ ๋ฅ ๋ค์ด๋ธ, 0์ฃผ์ฐจ-2
์คํฐ๋: ์๊ฐ CS, https://github.com/monthly-cs/2024-03-modern-react-deep-dive
์ค๋ ์งํ: ๊ฐ์ธ๊ณต๋ถ
DIL-week0-2_2024-03-03
| DIL ์ฃผ์ฐจ | ๋ฒ์ | ๋ฐํ ์ค๋น | ๋ด์ฉ | ์ค๋์ฐจ ์ง๋ | | -------- | ------ | --------- | ------------------------------ | ----------- | | 0์ฃผ์ฐจ | 1, 2์ฅ | B์กฐ: 2์ฅ | ๋ฆฌ์กํธ ํต์ฌ์์์ ์๋ฐ์คํฌ๋ฆฝํธ | 20p ~ 26p์ค |
์ค๋ ์ฝ์ ๋ด์ฉ์ markdown์ผ๋ก ๊ฐ๋จํ ๋ฉ๋ชจ
์ฝ์ ์๊ฐ: 5์ 50๋ถ~9์
๋ธ๋ก๊ทธ ์์ฑ: Medium blog
์์ ์์ฑ:./2024-03-03_sample.js
๋๋ฑ ๋น๊ต: props, exhaustive deps
-
props์ ๋๋ฑ ๋น๊ต
- ๊ฐ์ฒด์ ์์ ๋น๊ต
-
react์ ๋ชจ๋ ์์ ์ ๋๋ฑ ๋น๊ต๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ค
- ๊ฐ์๋ & ์ค์ ๋ ๋น๊ต, ๋ฆฌ๋๋๋ง, ๋ฉ๋ชจ์ด์ ์ด์ etc
-
effect hook & props & dependencies๋?
- https://react.dev/reference/react/useEffect#examples-dependencies
-
eslint-plugin-react-hooks์ exhaustive deps ๊ท์น ์ฌ์ฉ์ ๋ํด์
-
https://legacy.reactjs.org/docs/hooks-effect.html
Note
If you use this optimization, make sure the array includes all values from the component scope (such as props and state) that change over time and that are used by the effect. Otherwise, your code will reference stale values from previous renders. Learn more about how to deal with functions and what to do when the array changes too often.If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesnโt depend on any values from props or state, so it never needs to re-run. This isnโt handled as a special case โ it follows directly from how the dependencies array always works.
If you pass an empty array ([]), the props and state inside the effect will always have their initial values. While passing [] as the second argument is closer to the familiar componentDidMount and componentWillUnmount mental model, there are usually better solutions to avoid re-running effects too often. Also, donโt forget that React defers running useEffect until after the browser has painted, so doing extra work is less of a problem.
We recommend using the exhaustive-deps rule as part of our eslint-plugin-react-hooks package. It warns when dependencies are specified incorrectly and suggests a fix.
์ฐธ๊ณ
์ด ์ต์ ํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ์ปดํฌ๋ํธ ๋ฒ์(์: props ๋ฐ state)์์ ์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ๋ณ๊ฒฝ๋๋ ๋ชจ๋ ๊ฐ์ ๋ฐฐ์ด์ ํฌํจํด์ผ ํ๋ฉฐ ํด๋น ๊ฐ๋ค์ ํจ๊ณผ(effect)์์ ์ฌ์ฉ๋ฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์ฝ๋๊ฐ ์ด์ ๋ ๋๋ง์์์ ์ค๋๋ ๊ฐ๋ค์ ์ฐธ์กฐํ ์ ์์ต๋๋ค. ํจ์ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ ๋ฐ ๋ฐฐ์ด์ด ๋๋ฌด ์์ฃผ ๋ณ๊ฒฝ๋ ๋ ์ด๋ป๊ฒ ํด์ผ ํ๋์ง์ ๋ํด ์์ธํ ์์๋ณด์ธ์.
ํ ๋ฒ๋ง ์คํํ๊ณ ์ ๋ฆฌ(clean up)ํ๊ธฐ๋ฅผ ์ํ๋ค๋ฉด(๋ง์ดํธ์ ์ธ๋ง์ดํธ ์), ๋ ๋ฒ์งธ ์ธ์๋ก ๋น ๋ฐฐ์ด([])์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด React์๊ฒ ๋น์ ์ ํจ๊ณผ๊ฐ props๋ state๋ก๋ถํฐ ์์กดํ์ง ์์์ ์๋ฆฝ๋๋ค. ๋ฐ๋ผ์ ๋ค์ ์คํํ ํ์๊ฐ ์์ต๋๋ค. ์ด๊ฒ์ ํน๋ณํ ๊ฒฝ์ฐ๋ก ์ฒ๋ฆฌ๋์ง ์์ผ๋ฉฐ ํญ์ ์์กด์ฑ ๋ฐฐ์ด ์๋ ๋ฐฉ์์ผ๋ก ์ง์ ์ ์ผ๋ก ๋ฐ๋ฆ ๋๋ค.
๋น ๋ฐฐ์ด([])์ ์ ๋ฌํ๋ฉด ํจ๊ณผ ๋ด๋ถ์ props์ state๋ ํญ์ ์ด๊ธฐ๊ฐ์ ๊ฐ์ง๋๋ค. []๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฒ์ ์ต์ํ componentDidMount์ componentWillUnmount ๋ฉํ๋ชจ๋ธ์ ๊ฐ๊น์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ๋๋ฌด ์์ฃผ ํจ๊ณผ๋ฅผ ๋ค์ ์คํํ๋ ๊ฒ์ ํผํ๊ธฐ ์ํ ๋ ๋์ ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค. ๊ฒ๋ค๊ฐ React๋ useEffect๋ฅผ ๋ธ๋ผ์ฐ์ ๊ฐ ๊ทธ๋ฆผ์ ๊ทธ๋ฆฐ ํ๊น์ง ์ฐ๊ธฐํ๋ฏ๋ก ์ถ๊ฐ ์์ ์ ๋ฌธ์ ์ผ ๊ฐ๋ฅ์ฑ์ด ์ ์ด์ง๋๋ค.
eslint-plugin-react-hooks ํจํค์ง์ exhaustive-deps ๊ท์น ์ฌ์ฉ์ ๊ถ์ฅํฉ๋๋ค. ์๋ชป๋ ์ข ์์ฑ์ด ์ง์ ๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ํ๊ณ ์์ ์ฌํญ ์ ์ํฉ๋๋ค.
-
์์ํ ํ์ * 7
1. undefined
(1)์ ์ธํ ๋ณ์์ ๊ฐ์ด ํ ๋น๋์ง ์์๊ฑฐ๋, ๊ฐ์ด ์ฃผ์ด์ง์ง ์์ ์ธ์์ ์๋์ผ๋ก ํ ๋น๋๋ ๊ฐ์ ๋๋ค
2. null
๋ช ์์ ์ผ๋ก ๊ฐ์ด ์๊ฑฐ๋, ๋น์ด์๋ ๊ฐ์ด๋ค. ํน์ง=>typeof๋ก ํ์ธํ๋ฉด object
3. Boolean: ์ฐธ๊ณผ ๊ฑฐ์ง๋ง์ ๊ฐ์ง๋ ๋ฐ์ดํฐ ํ์ ์ด๋ค.
- ํ์์์
- (1)falsy๊ฐ? false, 0, -0, 0x0n, NaN, "", null, undefined, etc...
- (2)truthy? {}, []๋ truthy
4. Number
-(2์ 53์น) -1
~2์ 53์น-1
(-9,007,199,254,740,993 ~ 9,007,199,254,740,991)
MDN: Integer range for Number JSON์ผ๋ก ์ง๋ ฌํ๋ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ๋ฌธ ๋ถ์ํ ๋, ์ด ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ์ ์ ๊ฐ์ JSON ํ์๊ฐ ์ซ์ ์ ํ์ผ๋ก ๊ฐ์ ๋ณํํ ๋ ์์๋ ์ ์์ต๋๋ค. ๊ฐ๋ฅํ ํด๊ฒฐ์ฑ ์ ๋์ ๋ฌธ์์ด์ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ๋ ํฐ ์ซ์๋ BigInt ์ ํ์ ์ฌ์ฉํ์ฌ ํํํ ์ ์์ต๋๋ค.
-
Number's static properties
const biggestNum = Number.MAX_VALUE; const smallestNum = Number.MIN_VALUE; const infiniteNum = Number.POSITIVE_INFINITY; const negInfiniteNum = Number.NEGATIVE_INFINITY; const notANum = Number.NaN; console.log(biggestNum); // 1.7976931348623157e+308 console.log(smallestNum); // 5e-324 console.log(infiniteNum); // Infinity console.log(negInfiniteNum); // -Infinity console.log(notANum); // NaN
-
์์ ์ฝ๋
const maxInt = Math.pow(2, 53) - 1; console.log("maxInt in safe range", maxInt === Number.MAX_SAFE_INTEGER); const minInt = -(Math.pow(2, 53) - 1); console.log("minInt in safe range", minInt === Number.MIN_SAFE_INTEGER); // ์๋ฐ์คํฌ๋ฆฝํธ number์ ํ๊ณ const outOfRangeA = biggestNum + 1; const outOfRangeB = biggestNum + 100; console.log( "biggestNum + 1 === biggestNum + 100", outOfRangeA === outOfRangeB );
-
24p ์์์ฝ๋ ๊ด๋ จ ํ์ผ:
./2024-03-03_sample.js
5. Bigint
- [ES2020], ๋์ n ๋ถ์ด๊ฑฐ๋, Bigint() ํจ์ ์ฌ์ฉ
6. String
- ๋ฌธ์์ด์ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ๋ค. โ
foo[0] = 'a'
console.log(Object.getOwnPropertyDescriptors(foo));
{ configurable: false, // ์ฌ์ ์ ๋ถ๊ฐ๋ฅ enumerable: true, // ์ด๊ฑฐ ๊ฐ๋ฅ value: "c", // char writable: false // ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ }[]
- ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด, String and Iterator (string iterator object): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator
7. Symbol
- [ES6] ์ค๋ณต๋์ง ์๋ ๊ณ ์ ํ ๊ฐ, ๋ฐ๋์ Symbel() ํจ์ ์ฌ์ฉ
- ๋์ผํ ์ธ์๋ฅผ ๋๊ฒจ์ฃผ๋๋ผ๋ ๋์ผํ ๊ฐ x
- ๋์ผํ ๊ฐ์ ์ฌ์ฉํ ๋๋ Symbol.for์ ํ์ฉ
โ๏ธ ์์ด๊ณต๋ถ
exhaustive /iษกหzรดstiv/: examining, including, or considering all elements or aspects; fully comprehensive.
coerce /kลหษrs/: ๊ฐ์ ํ๋ค to do something by using force or threats.
type coercion coยทerยทcion /kลหษrSH(ษ)n,kลหษrZH(ษ)n/