Frontend/React

[Redux] ๋ฆฌ๋•์Šค ์›๋ฆฌ ์ดํ•ดํ•˜๊ธฐ

rachel_13 2022. 8. 13. 14:49

๐Ÿค” ๋ฆฌ๋•์Šค๋ž€ ๋ชฐ๊นŒ..? ๊ทธ๋ฆฌ๊ณ  ์™œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ๊นŒ?

https://ko.redux.js.org/

 

Redux - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•ฑ์„ ์œ„ํ•œ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ์ƒํƒœ ์ปจํ…Œ์ด๋„ˆ. | Redux

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•ฑ์„ ์œ„ํ•œ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ์ƒํƒœ ์ปจํ…Œ์ด๋„ˆ.

ko.redux.js.org

ํ”„๋กœ์ ํŠธ์˜ depth ๊ฐ€ ๊นŠ์–ด์ง€๋ฉด์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ„์— state๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ณ , ๋„˜๋‚˜๋“œ๋Š” ๊ฒƒ์ด ์ ์  ๋ณต์žกํ•ด์ง„๋‹ค..

์ด๋ฅผ ํ•ด๊ฒฐ ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋กœ ํ•œ๊บผ๋ฒˆ์— ์„ ์–ธํ•ด์„œ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, state๋กœ ์ผ์ผํžˆ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค!

redux cycle - store๋ฅผ ๊ธฐ์ค€์œผ๋กœ "๋‹จ๋ฐฉํ–ฅ"์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

๐Ÿ‘‰ ๋ฆฌ๋•์Šค์˜ ์žฅ์ 

- action์„ ์‹คํ–‰ํ•  ๋•Œ dispatch ์˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.(์—๋Ÿฌ ํ•ธ๋“ค๋ง์— ์šฉ์ด)

- git reset ์ฒ˜๋Ÿผ ์ด์ „ action์œผ๋กœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

   (๋‹จ, state๊ฐ€ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค๋Š” ์ „์ œ๊ฐ€ ์žˆ์–ด์•ผ ํ•จ)

 

๐Ÿ”ป๋‹จ์ ์€ ๋ญ๊ฐ€ ์žˆ์„๊นŒ?

- action๋“ค์ด ๋ฏธ๋ฆฌ ์ •์˜๊ฐ€ ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค. 

- ๋ถˆ๋ณ€์„ฑ์ด ์œ ์ง€๋˜์–ด์•ผ ํ•˜๋Š” state์˜ ํŠน์„ฑ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ๋งค๋ฒˆ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

 

๐Ÿ‘‰ ๋ฆฌ๋“€์„œ(reducer)..? ๊ทธ๊ฑด ๋˜ ๋ญ”๋ฐ?

๋ฆฌ๋•์Šค์˜ ๋‹จ์ ์„ ๋ณด์™„, ๋ถˆ๋ณ€์„ฑ์˜ ์†์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฐœ๋…์ด๋‹ค.

action์ด ์‹คํ–‰๋œ ํ›„, ์ƒˆ๋กœ์šด ๊ฐ์ฒด(object)๋ฅผ ๋งŒ๋“œ๋Š” ์—ญํ• ์ด๋ฉฐ, ๊ธฐ์กด์— ์ €์žฅ๋œ state๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๋Œ€์ฒด๋œ๋‹ค.

- state์™€ action์„ ์ธ์ž๋กœ ๊ฐ€์ง€๊ณ , next_state ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

  (* ์ฆ‰, state์™€ action์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.)

 

๐Ÿ‘‰ dispatch์™€ reducer ์‚ฌ์ด์— ์žˆ์Œ : middleware

 

[Example]

(1) ์Šคํ† ์–ด ์ƒ์„ฑํ•˜๊ธฐ - createStore

const { createStore } = require('redux');

//๋ฆฌ๋“€์„œ๋Š” ํ•จ์ˆ˜
const reducer = () => {};

//์ดˆ๊ธฐ๊ฐ’
const initialState = {
  id: 1,
  name: 'John',
  age: 20,
  is_admin: null,
};

//์Šคํ† ์–ด ์ƒ์„ฑ createStore(๋ฆฌ๋“€์„œ, ์ดˆ๊ธฐ๊ฐ’, ...)
const store = createStore(reducer, initialState);

//์ฝ˜์†”๋กœ ์ฐ์–ด๋ณด๊ธฐ
console.log(store.getState());

vscode description of createStore

(2) action ์ •์˜ํ•˜๊ธฐ

//action ~ objectํ˜•
//์•ก์…˜์€ ์ถ”์ƒ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. (ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ)

const '์•ก์…˜ ์ •์˜' = {
	type: '์•ก์…˜ ์ด๋ฆ„',
	data: '๋ฐ”๊พธ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’',
	payload,
	error,
	result,
}

//๋™์ ์œผ๋กœ ์•ก์…˜์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜ํ˜•์œผ๋กœ ๋งŒ๋“  ๋ชจ์Šต - ์žฌ์‚ฌ์šฉ์„ฑ
const changeAction = (data) => {
	return { //action - ์•ก์…˜์€ object๋‹ค.
    	type: 'CAHNGE_A',
        data,
    }
}

(3) dispatch ์ ์šฉ

//(1) action ์ƒ์„ฑ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ
store.dispatch(changeAdminAccess('Jully'));


//(2) ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ
store.dispatch({
	type: 'CHANGE_USER_NAME',
	data: 'Jully',
});

(4) reducer ๋งŒ๋“ค๊ธฐ

const reducer = (prevState, action) => {
  //action ์„ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด state๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  //action ์ด ๋งŽ์•„์ง€๋ฉด case๋“ค์ด ์ ์  ๋Š˜์–ด๋‚จ
  switch (action.type) {
    case 'CHANGE_USER_NAME':
      return {
        id: 1,
        name: action.data,
        age: 20,
        is_admin: null,
      };
    case 'CHANGE_USER_AGE':
      return {
        id: 1,
        name: 'John',
        age: action.data,
        is_admin: null,
      };
  }
};