
JS ๊ธฐ๋ฐ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฑ ๋ณด์ฅ๋ณด์ผ๋ฌ ํ๋ ์ดํธ ์ฝ๋ ์์ด ๋๋ฌด ๋ง์.๋ฌ๋ ์ปค๋ธ ๋์.๋๋ฒ๊น
์ด ์ฝ๋ค.๋๊ท๋ชจ ํ๋ก์ ํธ์์ ๋ง์ด ์ฐ์์ํ๊ฐ ๊ด๋ฆฌ๋๋ ํ๋์ ๊ณต๊ฐ๋ฐ์ดํฐ (์ฃผ๋ฌธ์){
type: 'ACTION_CHANGE_USER', // ํ์
payload: { // ์ต์
name: 'ํ๋๋ชฌ',
age: 100
}
}Store์ ์ ๋ฌํ๊ธฐ ์ํด ๊ฑฐ์ณ์ผ ํ๋ ๊ณณdispatch() ๋ฉ์๋ ์ฌ์ฉํด์ผ ํจ.

React ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ง๊ด์ ์ด๊ธฐ ๋๋ฌธ์, ๋น๊ต์ ์ฌ์ฉ์ ์นํ์ ์.boilerplate-free API ์ ๊ณต๋ฌ๋ ์ปค๋ธ ๋ฎ์.data-flow graph**:** ์ํ ๋ฐ์ดํฐ๊ฐ atom โ selector โ ์ปดํฌ๋ํธ ์์๋ก ํ๋ฆ๋ฐํ์์์๋ ์์ฑ ๊ฐ๋ฅReact์ state ๋์ ์ฌ์ฉ ๊ฐ๋ฅ๋ชจ๋ ์ปดํฌ๋ํธ๋ ์ํ๋ฅผ ๊ณต์ ํจ.const fontSizeState = atom({
key: 'fontSizeState',
default: 14,
});
๊ณ ์ ํ ํค ๊ฐ ํ์useRecoilState ํ
์ ํตํด atom ์ฝ๊ณ ์ฐ๊ธฐ ๊ฐ๋ฅuseState์ ๋น์ทํ์ง๋ง, ์ปดํฌ๋ํธ ๊ฐ์ ์ํ๊ฐ ๊ณต์ ๋ ์ ์๋ค๋ ์ฐจ์ด.
function FontButton() {
const [fontSize, setFontSize] = useRecoilState(fontSizeState);
return (
<button onClick={() => setFontSize((size) => size + 1)} style={{fontSize}}>
Click to Enlarge
</button>
);
}
atom ์ํ ๊ฐ์ ๋๊ธฐ/ ๋น๋๊ธฐ ๋ฐฉ์์ ํตํด ๋ณํ์์ ์ ํ์๋ก ํ๋์ง, ์ด๋ค ์ํ์ ์์กดํ๋์ง ์ถ์ const fontSizeLabelState = selector({
key: 'fontSizeLabelState',
get: ({get}) => {
// fontSizestate๋ผ๋ ํ๋์ atom์ ์์กด์ฑ์ ๊ฐ์ง
const fontSize = get(fontSizeState);
const unit = 'px';
return `${fontSize}${unit}`;
},
});
function FontButton() {
const [fontSize, setFontSize] = useRecoilState(fontSizeState);
// Selectors๋ useRecoilValue() ํตํด ์ฝ์ ์ ์์
// fontSizeLableState๋ writable ํ์ง ์๊ธฐ ๋๋ฌธ์ useRecoilState()๋ฅผ ์ด์ฉํ์ง ์์!
const fontSizeLabel = useRecoilValue(fontSizeLabelState);
return (
<>
<div>Current font size: ${fontSizeLabel}</div>
<button onClick={setFontSize(fontSize + 1)} style={{fontSize}}>
Click to Enlarge
</button>
</>
);
}
Redux - ์๋ฐ์คํฌ๋ฆฝํธ ์ฑ์ ์ํ ์์ธก ๊ฐ๋ฅํ ์ํ ์ปจํ ์ด๋. | Redux
Redux(๋ฆฌ๋์ค)๋? (์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ) - ํ๋๋ชฌ