참고코드(redux-toolkit vs recoil): https://github.com/seungahhong/states-todos
리액트 상태 관리 로직의 한계점
Recoil은 왜 필요한 걸까??
Recoil 철학
redux, reducer, constant
useEffect(() => {
(async () => {
await fetch~~
-> redux 호출
})();
}, []);
const allMainFetch = selector({
key: 'allMainFetch',
get: async ({ get }) => await get(fetch~~)
})
});
const main = useRecoilValue(allMainFetch());
// 파생데이터
const firstAtom = atom({
key: 'firstAtomKey',
default: 1
});
const secondAtom = atom({
key: 'firstAtomKey',
default: 1
});
const cumulatedSelector = selector({
key: 'cumulatedSelector',
get: ({ get }) => {
return get(firstAtom) + get(secondAtom)
}
});
// atom
import {atom, useRecoilState} from 'recoil';
const counter = atom({
key: 'myCounter',
default: 0,
});
function Counter() {
const [count, setCount] = useRecoilState(counter);
const incrementByOne = () => setCount(count + 1);
return (
<div>
Count: {count}
<br />
<button onClick={incrementByOne}>Increment</button>
</div>
);
}