let's get IT with DAVINA ๐Ÿ’ป

Redux ๋ณธ๋ฌธ

DEV_IN/Library

Redux

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 9. 05:45

Redux๋ž€?

  • ์ƒํƒœ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Store์„ ์ œ๊ณตํ•จ์œผ๋กœ์จ ํ•˜๊ธฐ์™€ ๊ฐ™์€ ๋ฌธ์ œ ํ•ด๊ฒฐ
  • ์ปดํฌ๋„ŒํŠธ์™€ ์ƒํƒœ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ํŒจํ„ด

Redux์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

  1. ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ๋ณ€๊ฒฝ๋  ์ƒํƒœ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด Action ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
  2. ์ด Action๊ฐ์ฒด๋Š” Dispatch ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
  3. Dispatch ํ•จ์ˆ˜๋Š” Action ๊ฐ์ฒด๋ฅผ Reducer ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์ค๋‹ˆ๋‹ค.
  4. Reducerํ•จ์ˆ˜๋Š” Action ๊ฐ์ฒด์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ , ๊ทธ ๊ฐ’์— ๋”ฐ๋ผ ์ „์—ญ ์ƒํƒœ ์ €์žฅ์†Œ Store์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  5. ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด, React๋Š” ํ™”๋ฉด์„ ๋‹ค์‹œ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, Redux์—์„  Action→Dispatch→Reducer→Store ์ˆœ์„œ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ํ๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


redux ์„ค์น˜

npm install redux

1. store ์ƒ์„ฑ

store = date๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณณ

import {createStore} from "redux"

const store=createStore();

2. reducer ์ƒ์„ฑ

1๋ฒˆ ๊ณผ์ •์—์„œ createStore์€ reducer ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” error๊ฐ€ ๋œธ!

reducer = data๋ฅผ modifyํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋กœ, reducer๊ฐ€ returnํ•˜๋Š” ๊ฐ’์ด application์— ์žˆ๋Š” data๊ฐ€ ๋จ

import {createStore} from "redux"

const reducer = () => {};
const store = createStore(reducer);

3. action ์ƒ์„ฑ

reducerํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ data๋ฅผ ๋ณ€๊ฒฝํ• ์ง€ ๋ฐฉ๋ฒ•์„ ์ทจํ•ด์ฃผ๋Š” ๊ฐ์ฒด

{type: “ADD”}ํ˜•ํƒœ๋กœ ๊ฐ์ฒด์—ฌ์•ผ ํ•จ!!

import {createStore} from "redux"

const reducer = (count=0, action) => { //undefined์ด๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ๊ฐ’ ์ง€์ •
	if(action.type==="ADD") {
		return count+1;
	} else if(action.type==="MINUS"){
			return count-1;
		} else {
			return count
		}
};

const store = createStore(reducer);
//refactoring
import {createStore} from "redux"

const ADD = "ADD";  //์‹ค์ˆ˜๋กœ ์˜คํƒ€๋ฅผ ๋‚ผ ์‹œ, ์—๋Ÿฌ๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋กœ ์„ ์–ธ 
const MINUS = "MINUS";

const reducer = (count=0, action) => { //undefined์ด๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ๊ฐ’ ์ง€์ •
	switch(action.type){
		case ADD:
			return count+1;
		case MINUS:
			return count-1;
		default:
			return count;
	}
};

const store = createStore(reducer);

4. dispatch ์ƒ์„ฑ

reducerํ•จ์ˆ˜์—๊ฒŒ action๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์ค˜์„œ communicateํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

import {createStore} from "redux"

const reducer = (count=0, action) => { 
	if(action.type==="ADD") {
		return count+1;
	} else if(action.type==="MINUS"){
			return count-1;
		} else {
				return count
			}
};
const store = createStore(reducer);

store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"MINUS"});

console.log(store.getState()); //4๊ฐ€ ์ถœ๋ ฅ๋จ

5. subscribe ์ƒ์„ฑ

store ์•ˆ์— ์žˆ๋Š” ๋ณ€ํ™” ๊ฐ์ง€

const onChange = () => {
	number.innerText = store.getState();
}

store.subscribe(onChange);

Redux Hooks

  • Store, Reducer, Action, Dispatch ๊ฐœ๋…๋“ค์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• 
  • React-Redux์—์„œ Redux๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Hooks ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • useSelector()
    • useDispatch()

useDispatch()

useDispatch()๋Š” Action ๊ฐ์ฒด๋ฅผ Reducer๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” Dispatch ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” method์ž…๋‹ˆ๋‹ค. (์ƒํƒœ ๋ณ€ํ™˜)

import { useDispatch } from 'react-redux'

const dispatch = useDispatch()
dispatch( increase() )
console.log(counter) // 2

dispatch( setNumber(5) )
console.log(counter) // 5

useSelector()

useSelector()๋Š” ์ปดํฌ๋„ŒํŠธ์™€ state๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ Redux์˜ state์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” method์ž…๋‹ˆ๋‹ค. (์ƒํƒœ๊ฐ’ ์กฐํšŒ) 

// Redux Hooks ๋ฉ”์„œ๋“œ๋Š” 'redux'๊ฐ€ ์•„๋‹ˆ๋ผ 'react-redux'์—์„œ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
import { useSelector } from 'react-redux'
const counter = useSelector(state => state)
console.log(counter) // 1

[์˜ˆ๊ณ ํŽธ]
Redux Toolkit ? → Redux ์ฝ”๋“œ์–‘์„ ์ค„์ด๊ธฐ ์ข‹์€ ํŒจํ‚ค์ง€

 

'DEV_IN > Library' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Redux Toolkit  (4) 2023.02.21
Comments