๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React

[ React ] ์ƒํƒœ ๊ด€๋ฆฌ ํˆด ๋น„๊ต ๋ถ„์„ Recoil vs Zustand

 

 

 

ํšŒ์‚ฌ์—์„œ ์ƒˆ๋กœ์šด React ํ”„๋กœ์ ํŠธ๋ฅผ ์„ธํŒ…์ค‘์ธ๋ฐ, ์ „์—ญ ์ƒํƒœ๊ด€๋ฆฌ ํˆด ์ค‘์—์„œ ํŒ€์›๋“ค์ด ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๊ณ  ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์‰ฝ๊ฒŒ React ํ”„๋กœ์ ํŠธ์˜ ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํˆด 2๊ฐ€์ง€๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

 

 

 

 

Recoil (v0.7.7 ๊ธฐ์ค€)

 

https://recoiljs.org/ko/

 

 

์ „์ฒด ์นด์šดํ„ฐ ์ฝ”๋“œ 

counter sample code

 

 



Recoil์€ ์•„๋ž˜์ฒ˜๋Ÿผ ์“ฐ์ธ๋‹ค. (ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ผ๋ถ€)

import "./App.css";
import { countState } from "./state/countState";
import { useRecoilValue, useSetRecoilState, useResetRecoilState } from "recoil";

function App() {
  const count = useRecoilValue(countState);
  const setCount = useSetRecoilState(countState);
  const removeAllCounts = useResetRecoilState(countState);

  return (
    <div className="App">
      <div>
        <img
          src="https://recoiljs.org/img/logo.svg"
          className="logo"
          alt="recoil logo"
          style={{ backgroundColor: "#ffffff" }}
        />
      </div>
      <h1>Test Recoil !</h1>
      <div className="card">
        <button onClick={() => setCount((state) => state + 1)}>
          count is {count}
        </button>
      </div>
      <div className="card">
        <button onClick={removeAllCounts}>remove counts</button>
      </div>
    </div>
  );
}

export default App;

 

 

  • react์˜ useState์™€ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์„œ ์‰ฝ๊ฒŒ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Œ

  • ์ดˆ๊ธฐ ์„ธํŒ…์ด ํ•„์š”ํ•จ. (recoil์„ ์‚ฌ์šฉํ•  ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— RecoilRoot ์„ ์–ธ ๋“ฑ๋“ฑ)

  • state์— ๋Œ€ํ•œ ์•ก์…˜์ด component ๋‹จ์œ„๋กœ ์ผ์–ด๋‚˜์„œ ์•ก์…˜์— ๋Œ€ํ•œ ๊ด€๋ฆฌ๊ฐ€ ๊นŒ๋‹ค๋กœ์šธ ์ˆ˜ ์žˆ์Œ.

  • recoil๋งŒ์˜ state์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ์กด์žฌ (atom)

  • ์•ก์…˜์— ๋Œ€ํ•œ ์ปค์Šคํ…€ ๋„ค์ด๋ฐ ๋ถˆ๊ฐ€๋Šฅ. (์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ๋”ฐ๋กœ hook์„ ๋งŒ๋“ค๊ฑฐ๋‚˜, ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์จ์•ผํ•จ.)

 

 

 

 

 

 

 

 

 

 

 

 

Zustand (v4.3.8 ๊ธฐ์ค€)

 

 

https://github.com/pmndrs/zustand

 

 

import "./App.css";
import { useCountStore } from "./store/countStore";

function App() {
  const count = useCountStore((state) => state.count);
  const increaseCount = useCountStore((state) => state.increaseCount);
  const removeAllCounts = useCountStore((state) => state.removeAllCounts);

  return (
    <div className="App">
      <div>
        <img
          src="https://github.com/pmndrs/zustand/raw/main/bear.jpg"
          className="logo zustand"
          alt="zustand logo"
        />
      </div>
      <h1>Test Zustand !</h1>
      <div className="card">
        <button onClick={increaseCount}>count is {count}</button>
      </div>
      <div className="card">
        <button onClick={removeAllCounts}>remove counts</button>
      </div>
    </div>
  );
}

export default App;


์ „์ฒด ์นด์šดํ„ฐ ์ฝ”๋“œ 

counter sample code

 

 

 

  • ์—„์ฒญ ์‹ฌํ”Œํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

  • store์„ ์ปดํฌ๋„ŒํŠธ์™€ ๋ถ„๋ฆฌํ•˜์—ฌ state, get, set ๋“ฑ์˜ ์•ก์…˜๊นŒ์ง€ ํ•œ๋ฒˆ์— ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•จ.

  • ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ด.

  • ๋‚ด์žฅ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šคํ† ๋ฆฌ์ง€ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

  • state์— ๋Œ€ํ•œ ์•ก์…˜์— ๋Œ€ํ•ด ๋„ค์ด๋ฐ ๊ฐ€๋Šฅ.

 

 

 

 

 

 

 

 

 

 

-> ๊ธฐ์กด์— ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ์—์„œ Recoil์„ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜, Zustand๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋‹ˆ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์ „์—ญ ์ƒํƒœ์˜ ๊ด€๋ฆฌ๊ฐ€ Recoil๋ณด๋‹ค Zustand๊ฐ€ ์šฉ์ดํ•  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ํŒ๋‹จ์ด ๋˜์–ด, ์šฐ๋ฆฌ๋Š” Zustand๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

 


 

๐ŸŒธ ์ถœ์ฒ˜ ๐ŸŒธ

 

Zustand - Github
Recoil