ํ์ด์ค๋ถ์ด ๋ง๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(ํ์ง๋ง Redux๋ MobX, react-router์ ํจ๊ป ํ๋ ์์ํฌ์ฒ๋ผ ์ฌ์ฉ)
Redux ๊ณต๋ถ check
Component ์์คํ ์ ๊ตฌํํ๊ธฐ ๋๋ฌธ์ ํจ์จ์ ์ผ๋ก ์ฝ๋ฉ ๊ฐ๋ฅ๐ Component๋ ํ๋์ ์๋ฏธ๋ฅผ ๊ฐ์ง ๋ ๋ฆฝ์ ์ธ ๋จ์ ๋ชจ๋
์๋ฐ์คํฌ๋ฆฝํธ์ ํ์ฅ ๋ฌธ๋ฒ
1) ๋ฐ๋์ ํ๋์ ์๋ฆฌ๋จผํธ๋ก ๊ฐ์ธ์ผ ํ๋ค.
return (
<h1> Hello Velopert</h1>
<h2> Welcome </h2>
); // error
return (
<div>
<h1> Hello Velopert </h1>
<h2> Welcome </h2>
</div>
); // div element๋ฅผ wrapper๋ก ์ฌ์ฉ
2) ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ ์ฉํ ๋ { } ์ ์ฉ
3) JSX ๋ด๋ถ์์ if๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค. IIFE or ์ผํญ์ฐ์ฐ์ ์ฌ์ฉ
// condition์ด true์ด๋ฉด, ์ฐ์ฐ์ expr1์ ๊ฐ์ ๋ฐํ
var age = 29;
var canDrinkAlcohol = (age > 19) ? "True, over 19" : "False, under 19";
console.log(canDrinkAlcohol); // "True, over 19"
// ์ผํญ์ฐ์ฐ์
// &&
//์กฐ๊ฑด์ด true์ผ๋๋ง ๋ณด์ฌ์ง๊ณ false์ผ ๊ฒฝ์ฐ์๋ ์๋ฌด๊ฒ๋ ๋ณด์ฌ์ฃผ์ง ์๋๋ค.
1 + 1 === 2 && '๋ง์ต๋๋ค' // '๋ง์ต๋๋ค'
1 + 1 === 3 && '๋ง์ต๋๋ค' // false
// ||
// ์กฐ๊ฑด์ด false๋ณด์ฌ์ง๊ณ true ๊ฒฝ์ฐ์๋ ์๋ฌด๊ฒ๋ ๋ณด์ฌ์ฃผ์ง ์๋๋ค.
1 + 1 === 3 || 'ํ๋ฆฝ๋๋ค' // 'ํ๋ฆฝ๋๋ค'
1 + 1 === 2 || 'ํ๋ฆฝ๋๋ค' // true
4) className์ ์ฌ์ฉ
1. Data Flow : ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ, ํ ๋ฐฉํฅ์ผ๋ก๋ง ๋ฐ์ดํฐ ์ด๋
2. Props
์์ ์ปดํฌ๋ํธ๊ฐ ํ์ ์ปดํฌ๋ํธ์๊ฒ ๋ด๋ ค์ฃผ๋ ๋ฐ์ดํฐ, ๋จ์ํ ์ฌ์ฉ๋ง ๊ฐ๋ฅ, ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ, ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์ ๋ฌํ๋ props๊ฐ ๋ฐ๋๋ฉด ์๋์ผ๋ก ์ ๋ฐ์ดํธ, props๋ html์ฒ๋ผ ๊ธฐ๋ณธ์ด ๋ฌธ์์ด์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์๋ฃํ์ ์ ๋ฌํ๊ธฐ ์ํด์๋ { } ์์ ๋ฃ์ด์ผ ํ๋ค.
3. State
์ปดํฌ๋ํธ๊ฐ ๊ฐ๋ ์ํ, ๊ฐ์ฒด์ ํํ๋ก ์ปดํฌ๋ํธ ๋ด์์ ๋ณด๊ด / ๊ด๋ฆฌ, Class ์ปดํฌ๋ํธ๋ก ์์ฑ, ๊ฐ์ ๋ณ๊ฒฝํ ๋ ๋ฐ๋์ setState ๋ฉ์๋๋ฅผ ์ฌ์ฉ
4. setState
state๋ฅผ ๋ฐ๊พธ๋ ค๋ฉด
this.state.์ด๋ฆ = ๋ฐ๊ฟ ๊ฐ
์ด ์๋๋ผthis.setState((์ด์ ์ํ) => ({ ์ด๋ฆ: ๋ฐ๊ฟ ๊ฐ }));
์ปดํฌ๋ํธ๊ฐ ์คํ๋๊ฑฐ๋ ์ ๋ฐ์ดํธ ๋๊ฑฐ๋ ์ ๊ฑฐ๋ ๋, ํน์ ํ ์ด๋ฒคํธ๋ค์ด ๋ฐ์
โ Mount
์ปดํฌ๋ํธ๊ฐ ์ฒ์ ์คํ๋ ๋ ๊ทธ๊ฒ์ Mount๋ผ๊ณ ํํํ๋ค. ์ปดํฌ๋ํธ๊ฐ ์์๋๋ฉด ์ฐ์ context, defaultProps์ state๋ฅผ ์ ์ฅํ๋ค. ๊ทธ ํ์ componentWillMount ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ render๋ก ์ปดํฌ๋ํธ๋ฅผ DOM์ ๋ถ์ฐฉํ ํ Mount๊ฐ ์๋ฃ๋ ํ componentDidMount๊ฐ ํธ์ถ๋๋ค.
์ฃผ์ํ ์ ์, componentWillMount์์๋ props๋ state๋ฅผ ๋ฐ๊พธ๋ฉด ์ ๋๋ค. Mount ์ค์ด๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์์ง DOM์ renderํ์ง ์์๊ธฐ ๋๋ฌธ์ DOM์๋ ์ ๊ทผํ ์ ์๋ค.
componentDidMount์์๋ DOM์ ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ทธ๋์ ์ฌ๊ธฐ์์๋ ์ฃผ๋ก AJAX ์์ฒญ์ ํ๊ฑฐ๋, setTimeout, setInterval๊ฐ์ ํ๋์ ํฉ๋๋ค.
๋ค์ ์์๋ก ์คํ๋ฉ๋๋ค.state, context, defaultProps ์ ์ฅ
componentWillMount
render
componentDidMount
โ Props Update
props๊ฐ ์ ๋ฐ์ดํธ๋ ๋์ ๊ณผ์ ์ด๋ค. ์ ๋ฐ์ดํธ๋๊ธฐ ์ ์ ์ ๋ฐ์ดํธ๊ฐ ๋ฐ์ํ์์์ ๊ฐ์งํ๊ณ , componentWillReceiveProps ๋ฉ์๋๊ฐ ํธ์ถ๋๋ค. ๊ทธ ํ shouldComponentUpdate, componentWillUpdate๊ฐ ์ฐจ๋ก๋๋ก ํธ์ถ๋ ํ, ์ ๋ฐ์ดํธ๊ฐ ์๋ฃ(render)๋๋ฉด componentDidUpdate๊ฐ ๋๋ค. ์ด ๋ฉ์๋๋ค์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ๋ฐ๋ props์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. componentDidUpdate๋ง ์ด๋ฏธ ์ ๋ฐ์ดํธ๋์๊ธฐ ๋๋ฌธ์ ๋ฐ๋๊ธฐ ์ด์ ์ props์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
shouldcomponentUpdate์์๋ ์์ง renderํ๊ธฐ ์ ์ด๊ธฐ ๋๋ฌธ์ return false๋ฅผ ํ๋ฉด render์ ์ทจ์ํ ์ ์๋ค.
componentWillUpdate์์๋ state๋ฅผ ๋ฐ๊ฟ์๋ ์ ๋๋ค. ์์ง props๋ ์ ๋ฐ์ดํธํ์ง ์์์ผ๋ฏ๋ก state๋ฅผ ๋ฐ๊พธ๋ฉด ๋ shouldComponentUpdate๊ฐ ๋ฐ์ํ๋ค. componentDidUpdate์์๋ render์ด ์๋ฃ๋์๊ธฐ ๋๋ฌธ์ DOM์ ์ ๊ทผํ ์ ์๋ค.componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
โ State Update
setState ํธ์ถ์ ํตํด state๊ฐ ์ ๋ฐ์ดํธ๋ ๋์ ๊ณผ์ ์ด๋ค. props update์ ๊ณผ์ ์ด ๊ฐ์ง๋ง, componentWillReceiveProps ๋ฉ์๋๋ ํธ์ถ๋์ง ์๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฉ์๋์ ๋ ๋ฒ์งธ ์ธ์๋ก๋ ๋ฐ๋ state์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. componentDidUpdate๋ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฐ๋๊ธฐ ์ด์ ์ state์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
โ Unmount
์ปดํฌ๋ํธ๊ฐ ์ ๊ฑฐ๋๋ ๊ฒ์ Unmount๋ผ๊ณ ํํํฉ๋๋ค. ๋๋ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ง ์์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ๊ฐ ์์ต๋๋ค. componentWillUnmount๊ฐ ๊ทธ๊ฒ์ ๋๋ค. componentDidUnmount๋ ์์ต๋๋ค. ์ด๋ฏธ ์ ๊ฑฐ๋ ์ปดํฌ๋ํธ์์ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํฌ ์๋ ์๊ฒ ์ฃ ? componentWillMount์์ ์ฃผ๋ก ์ฐ๊ฒฐํ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ ๊ฑฐํ๋ ๋ฑ์ ์ฌ๋ฌ ๊ฐ์ง ์ ๋ฆฌ ํ๋์ ํฉ๋๋ค.
componentWillUnmount