Recoil์ React๋ฅผ ์ํ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
Atom์ ์ปดํฌ๋ํธ ๊ฐ์ ๊ณต์ ๋๋ ์ํ๋ฅผ ๋ํ๋ธ๋ค. ์ด๋ฌํ Atom์ด ์ ๋ฐ์ดํธ ๋ ๊ฒฝ์ฐ ํด๋น Atom์ ์ํฅ์ด ์๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ค์ ์ํ๊ฐ ์๋ก์ด ๊ฐ์ผ๋ก ๋ฆฌ๋ด๋ ๋๋ค.
์ฌ์ฉํ๊ธฐ์ ์์ Atom์ ์ ์ํด์ค ํ์๊ฐ ์๋ค.
// atoms.js
import { atom } from 'recoil';
export const counterState = atom({
key: 'counterState',
default: 0,
});
์ ์ํ Atomd์ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ์์ useRecoilState์ ์ด์ฉํ์ฌ ์ํ๋ฅผ ์ฝ๊ณ ์ธ ์ ์๋ค.
// CounterComponent.js
import React from "react";
import { useRecoilState } from "recoil";
import { counterState } from "./atoms";
function CounterComponent() {
const [counter, setCounter] = useRecoilState(counterState);
const increaseCounter = () => setCounter(counter + 1);
const decreaseCounter = () => setCounter(counter - 1);
return (
<div>
<p>Counter: {counter}</p>
<button onClick={increaseCounter}>Increase</button>
<button onClick={decreaseCounter}>Decrease</button>
</div>
);
}
export default CounterComponent;
import React from "react";
import { RecoilRoot } from "recoil";
import CounterComponent from "./component/CounterComponent";
function App() {
return (
<RecoilRoot>
<CounterComponent />
</RecoilRoot>
);
}
export default App;
import React from "react";
import { RecoilRoot } from "recoil";
import CounterComponent from "./component/CounterComponent";
function App() {
return (
<CounterComponent />
);
}
export default App;
useRecoilState์ ๋ฌ๋ฆฌ useRecoilValue๋ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ ํจ์์์ด 'Atom' ๊ฐ๋ง ๋ฐ๋๋ค.
// DisplayComponent.js
import React from "react";
import { useRecoilValue } from "recoil";
import { counterState } from "./atoms";
function DisplayComponent() {
const counter = useRecoilValue(counterState);
return (
<div>
<p>Counter Value: {counter}</p>
</div>
);
}
export default DisplayComponent;
import React from "react";
import { RecoilRoot } from "recoil";
import DisplayComponent from "./component/DisplayComponent";
function App() {
return (
<RecoilRoot>
<DisplayComponent />
</RecoilRoot>
);
}
export default App;
Atom์ ๊ธฐ๋ฐ์ผ๋ก ์๋ก์ด ๊ฐ์ ๋ฆฌํดํ๊ฑฐ๋ ๊ธฐ์กด atom์ ๊ฐ์ ์์ ํ๋ ์ญํ ์ ์ํํ๋ค.
Atom์ ๊ฐ์ด ์ต์ ํ๋๋ฉด ์๋์ผ๋ก selector์ ๊ฐ๋ ์ต์ ํํ๋ฏ๋ก ๊ด๋ฆฌํ๊ธฐ์ ๊ฐํธํ๋ค.
import { atom, selector } from "recoil";
export const counterState = atom({
key: "counterState",
default: 1,
});
export const doubleCounterState = selector({
key: "doubleCounterState",
get: ({ get }) => {
const counter = get(counterState);
return counter * 2;
},
});
import React from "react";
import { useRecoilValue } from "recoil";
import { doubleCounterState } from "./atoms";
function CounterWithDoubleComponent() {
const doubleCounter = useRecoilValue(doubleCounterState);
return (
<div>
<p>Double Counter: {doubleCounter}</p>
</div>
);
}
export default CounterWithDoubleComponent;
import React from "react";
import { RecoilRoot } from "recoil";
import CounterWithDoubleComponent from "./component/CounterWithDoubleComponent";
import CounterComponent from "./component/CounterComponent";
function App() {
return (
<RecoilRoot>
<CounterWithDoubleComponent />
<CounterComponent />
</RecoilRoot>
);
}
export default App;
๐ ์์ง๊น์ง Selector์ useRecoilState๋ฅผ ์ฌ์ฉํ ์ ์๊ณ , useRecoilValue๋ฅผ ์ด์ฉํด์ ๋ฆฌํด๋๋ ๊ฐ๋ง ๋ฐ์ ์ ์๋ค. (2021)
npx create-react-app .
npm install recoil