header-img
Info :
728x90

 

 
์šฐ์„ .. ์ž˜๊ฐ€๋ผ..

์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ Redux ๋ฅผ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€... ๋ฌธ์ œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด์„œ ๋ญ”๊ฐ€ ๋‚ด๊ฐ€ ์ˆ˜์ •ํ•ด์„œ ์“ฐ๊ธฐ์—๋Š” ๊ตฌ์กฐ๊ฐ€ ๋˜๊ฒŒ ๋ณต์žกํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

๊ฑฐ๊ธฐ์„œ๋ถ€ํ„ฐ ๋“  ์˜๋ฌธ์ ์ด ์™œ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ Redux๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๊ฐ€์˜€๊ณ  (๊ตฌํ˜„ ์šฉ์ด์„ฑ์€ ๋–จ์–ด์ง€๋‚˜ ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚˜๊ฒŒ ์ข‹๋‹ค๋˜์ง€ ๊ทธ๋Ÿฐ ์žฅ์ ์ด ์žˆ๋Š”๊ฐ€๋ฅผ ์„œ์นญํ•˜๊ธฐ ์‹œ์ž‘ํ•จ.. ), ๊ทธ ์ด์œ ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ์ž๋“ค์ด Redux๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ ๋‹น์—ฐํžˆ ๊ธฐ์ˆ ์„ ์Šต๋“ํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” '์‚ฌ์šฉ์ž๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ = ์ข‹์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ˜น์€ ๋ฏฟ์„๋งŒํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ'๋ผ๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ธ ๋“ฏํ•˜๋‹ค.

 

Redux๋Š” ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ž‘์„ฑํ•ด์•ผํ•˜๋Š” ์•„์ฃผ ํฐ ๋‹จ์ ์ด ์žˆ๋Š”๋ฐ ํ•„์š”์— ๋”ฐ๋ผ Redux๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์„œ๋น„์Šค ์‚ฌ์šฉ๋Ÿ‰์ด ๊ทธ๋ ‡๊ฒŒ ๊ฑฐ๋Œ€ํ•˜์ง€ ์•Š์€ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ํŠน์ • ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฝ์šฐ์—๋Š” '์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ' ๊ธฐ๋Šฅ ํ•˜๋‚˜๋งŒ์„ ์œ„ํ•ด Redux ์ฒด์ œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ๊ณผํ•˜์ง€๋Š” ์•Š์€๊ฐ€

 

์•„๋ฌดํŠผ ๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์ด๋ฒˆ์— ๊ฐœ๋ฐœํ•  ๊ฒƒ์€ ์™„์ „ ๋Œ€๊ทœ๋ชจ ์„œ๋น„์Šค ๋„ ์•„๋‹Œ๋ฐ๋‹ค๊ฐ€, ์•„๋ฌด๋ฆฌ ๋Œ€์šฉ๋Ÿ‰ ์„œ๋น„์Šค์— ์ ์šฉํ•˜๊ธฐ๋Š” ์•„์ง์€ ๋ฌด๋ฆฌ๋ผ๋Š” ํŒ๋‹จ๋“ค์ด ์žˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ facebook ์—์„œ๋„ Recoil๋กœ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•ด๋’€๋Š”๋ฐ, ํ•œ์ฐฝ ๋•Œ์˜ facebook๋ณด๋‹ค ํŠธ๋ž˜ํ”ฝ์„ ๋„˜์–ด๊ฐ€๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์งค ๊ฒฝ์šฐ๊ฐ€ ์‰ฝ๊ฒŒ ์žˆ์œผ๋ ค๋‚˜ ํ•˜๋Š” ์ƒ๊ฐ๋„ ๋“ ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ณผ๊ฐํ•˜๊ฒŒ ์–ผ๋ ˆ๋ฒŒ๋ ˆ ๊ตฌํ˜„ํ•ด๋‘์—ˆ๋˜.. redux saga ์™€ slice๋ฅผ ์‚ญ์ œํ•˜๊ณ ... Recoil๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 

About Redux.

1. React ์ „์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. : React ๋‚ด๋ถ€ ๊ด€์ ์—์„œ๋Š” ์™ธ๋ถ€์š”์ธ์œผ๋กœ Store๊ฐ€ ์ทจ๊ธ‰๋˜๋ฉฐ, ๋™์‹œ์„ฑ ๋ชจ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์— ํ˜ธํ™˜์„ฑ์ด ๋ถ€์กฑํ•˜๋‹ค๊ณ  ์—ฌ๊ฒจ์ง„๋‹ค.

2. ๋ณต์žกํ•œ Boiler Plate ์ดˆ๊ธฐ์„ธํŒ…์ด ์š”๊ตฌ๋œ๋‹ค. : Store, Action, Reducer ๋“ฑ ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํ•„์š”ํ•ด ๋น„ํšจ์œจ์ ์ด๋ฉฐ ๋Ÿฌ๋‹์ปค๋ธŒ๊ฐ€ ๋†’๋‹ค.

3. ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€ ๋ฆฌ์†Œ์Šค๊ฐ€ ์š”๊ตฌ๋œ๋‹ค.: Redux-saga ๋“ฑ ์ „์—ญ ์ƒํƒœ์— ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ 3rd Party ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

์ด๋Ÿฐ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์˜จ React์— ์ตœ์ ํ™” ๋œ ์ „์—ญ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ฐ”๋กœ Recoil ์ด๋ผ๊ณ  ํ•œ๋‹ค. 

 


 

ํŽ˜์ด์Šค๋ถ์—์„œ ๋งŒ๋“  ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, usestatue๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๋งŒํผ ์‚ฌ์šฉ์ด ๊ฐ„๋‹จํ•˜๋ฉด์„œ๋„ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

์ „์—ญ์ƒํƒœ์˜ ์„ค์ •/์ •์˜๊ฐ€ ๋งค์šฐ ์‰ฌ์šฐ๋ฉฐ, Recoil์ด ์ง€์›ํ•˜๋Š” Hooks๋กœ ์ด๋ฅผ get/setํ•˜๊ธฐ ๋•Œ๋ฌธ์— React ๋ฌธ๋ฒ•๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

๊ณต์‹ Document

https://github.com/facebookexperimental/Recoil

 

GitHub - facebookexperimental/Recoil: Recoil is an experimental state management library for React apps. It provides several cap

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features o...

github.com

1. ๊ณต์œ ์ƒํƒœ(Shared State)๋„ React ๋‚ด๋ถ€์ƒํƒœ(Local State)์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•œ get/set ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Boilerplate-free API๋ฅผ ์ œ๊ณต

2. ๋™์‹œ์„ฑ ๋ชจ๋“œ(Concurrent Mode)๋ฅผ ๋น„๋กํ•œ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด React์˜ ๊ธฐ๋Šฅ๋“ค๊ณผ์˜ ํ˜ธํ™˜ ๊ฐ€๋Šฅ์„ฑ

3. ์ƒํƒœ ์ •์˜๊ฐ€ ์ฆ๋ถ„ ๋ฐ ๋ถ„์‚ฐ๋˜๋ฏ€๋กœ ์ฝ”๋“œ ๋ถ„ํ• ์ด ๊ฐ€๋Šฅ

4. ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์ˆ˜์ •ํ•  ํ•„์š” ์—†์ด, ์ƒํƒœ์—์„œ ํŒŒ์ƒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. ๋˜ํ•œ, ํŒŒ์ƒ๋œ ๋ฐ์ดํ„ฐ๋Š” ๋™๊ธฐ/๋น„๋™๊ธฐ๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋Šฅ

5. ํƒ์ƒ‰์„ 1๊ธ‰ ๊ฐœ๋…์œผ๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‹ฌ์ง€์–ด ๋งํฌ์—์„œ ์ƒํƒœ ์ „ํ™˜์„ ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Œ.

6. ์—ญํ˜ธํ™˜์„ฑ ๋ฐฉ์‹์œผ๋กœ ์ „์ฒด ์•ฑ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์€ ์‰ฌ์šฐ๋ฏ€๋กœ, ์œ ์ง€๋œ ์ƒํƒœ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ณ€๊ฒฝ์—๋„ ์‚ด์•„๋‚จ์„ ์ˆ˜ ์žˆ์Œ. (์บ์‹ฑ)

 

 

Recoil์˜ ํŠน์ง•

1. ์ž‘๊ณ  React์Šค๋Ÿฌ์šด: "Recoil์€ React์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ์•ฑ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋น ๋ฅด๊ณ  ์œ ์—ฐํ•˜๊ฒŒ ๊ณต์œ ๊ฐ€ ๋˜๋Š” ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ผ"

2. ๋ฐ์ดํ„ฐ ํ๋ฆ„ ๊ทธ๋ž˜ํ”„: "ํŒŒ์ƒ ๋ฐ์ดํ„ฐ์™€ ๋น„๋™๊ธฐ ์ฟผ๋ฆฌ๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜์™€ ํšจ์œจ์ ์ธ ๊ตฌ๋…์œผ๋กœ ๊ด€๋ฆฌ๊ฐ€ ๋œ๋‹ค."

3. ๊ต์ฐจํ•˜๋Š” ์•ฑ ๊ด€์ฐฐ: "์ฝ”๋“œ ๋ถ„ํ• ์„ ์†์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ์•ฑ ์ „์ฒด์˜ ๋ชจ๋“  ์ƒํƒœ ๋ณ€๊ฒฝ์„ ๊ด€์ฐฐํ•˜์—ฌ ์ง€์†์„ฑ, ๋ผ์šฐํŒ…, ์‹œ๊ฐ„ ์ด๋™ ๋””๋ฒ„๊น… ๋˜๋Š” ์‹คํ–‰ ์ทจ์†Œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค."

 

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

 


https://recoiljs.org/

 

Recoil

A state management library for React.

recoiljs.org

 

 


๐Ÿ‘ผ Recoil ์‹œ์ž‘ํ•˜๊ธฐ 

 

1. Install ์„ค์น˜

// npm(yarn)
npm install recoil
yarn add recoil

// CDN
<script src="https://cdn.jsdelivr.net/npm/recoil@0.0.11/umd/recoil.production.js"></script>

* Recoil์€ Webpack๊ณผ ๊ฐ™์€ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ์™€ ํ˜ธํ™˜์ด ๋˜๋ฉฐ ES6 -> 5 ๋กœ๋Š” ํŠธ๋žœ์ŠคํŒŒ์ผ๋ง ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง•์„ ์œ„ํ•ด Babel์„ ํ†ตํ•œ ์ฝ”๋“œ ์ปดํŒŒ์ผ ๊ณผ์ •์ด ์š”๊ตฌ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

2. Apply ์ ์šฉ

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router-dom';
import reportWebVitals from './reportWebVitals';

import App from './App';
import { Provider } from "react-redux";
import store from "./utils/store";
import history from "./utils/history";

import {CookiesProvider} from 'react-cookie';
import './index.css';

import { RecoilRoot } from 'recoil';
// 1. recoil ์‚ฌ์šฉ ํฌ์ธํŠธ 1

ReactDOM.render(
  <RecoilRoot>							//2. recoil ์‚ฌ์šฉ ํฌ์ธํŠธ
    <React.StrictMode>
      <CookiesProvider>
        <Provider store={store}>
          <Router history={history}>
            <App />
          </Router>
        </Provider>
      </CookiesProvider>
    </React.StrictMode>
  </RecoilRoot>,						//2. recoil ์‚ฌ์šฉ ํฌ์ธํŠธ
  document.getElementById('root')
);

reportWebVitals();

์ฝ”๋“œ๊ฐ€ ๋งค์šฐ ๋•์ง€๋•์ง€์ด๋‹ค.

์•„๋ฌดํŠผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” recoil์„ ๋ถˆ๋Ÿฌ์™€์„œ index.js ํŒŒ์ผ์˜ <App/> ์ปดํฌ๋„ŒํŠธ๋ฅผ <RecoilRoot> ๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

3. Concept ํ•ต์‹ฌ๊ฐœ๋…

๋ณธ๊ฒฉ์ ์ธ ๊ตฌํ˜„์„ ์œ„ํ•˜์—ฌ ./src ๋‚ด์— recoil ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ „์—ญ์ƒํƒœ์— ๊ด€๋ จ๋œ Atoms, Selector ๋“ค์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

Recoil ์‚ฌ์šฉ ์‹œ
Atom(๊ณต์œ  ์ƒํƒœ)์—์„œ Selector(์ˆœ์ˆ˜ ํ•จ์ˆ˜)๋ฅผ ๊ฑฐ์ณ React Component๋กœ ๋‚ด๋ ค๊ฐ€๋Š” Data-flow Graph๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
- Atoms๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์˜ ๋‹จ์œ„
- Selectors๋Š” Atoms ์ƒํƒœ ๊ฐ’์„ ๋™๊ธฐ ํ˜น์€ ๋น„๋™๊ธฐ ๋ฐฉ์‹์„ ํ†ตํ•ด ๋ณ€ํ™˜

- Recoil์—์„œ ์ง€์›ํ•˜๋Š” Hooks๋“ค๋กœ atom ํ˜น์€ selector์˜ get(์ ‘๊ทผ) ๋ฐ set(์ˆ˜์ •) ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ๋™์ž‘์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

(1) Atoms ์„ค์ •

  key์™€ default๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

// /src/recoil/index.js

import { atom } from "recoil";

export const articleStates = atom({
    key: 'articleState',
    default: [],
})

export const commentStates = atom({
    key: 'commentState',
    default: [],
  })

* default ๊ฐ’์€ Promise ๊ฐ์ฒด๋„ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋‚˜, atom์—์„œ ๋ฐ”๋กœ ๋น„๋™๊ธฐ ์š”์ฒญ์„ ํ•  ์ˆ˜๋Š” ์—†๋‹ค. 

 

(2) ์ „์—ญ์ƒํƒœ ๊ด€๋ จ hooks

 

1. useRecoilState

    - Atom์˜ ์ƒํƒœ๋ฅผ ๊ตฌ๋…. useState Hook๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ƒํƒœ, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ƒํƒœ์— ๋Œ€ํ•œ setter ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.

2. useRecoilValue

   - setter ํ•จ์ˆ˜ ์—†์ด Atom์˜ ์ƒํƒœ๋งŒ ๋ฐ˜ํ™˜

3. useSetRecoilState

   - atom ์ƒํƒœ ์—†์ด setter ํ•จ์ˆ˜๋งŒ ๋ฐ˜ํ™˜

4. useResetRecoilState

   - atom ์ƒํƒœ๋ฅผ default ์ƒํƒœ๋กœ reset

๋”๋ณด๊ธฐ
FRONTEND/React