[TIL] Day52 #Redux

Beanxxยท2022๋…„ 7์›” 7์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
52/120
post-thumbnail

2022.07.07(Thurs)

[TIL] Day52
[SEB FE] Day51

โ˜‘๏ธย Redux ์ข…ํ•ฉ Quiz

โœ๏ธย Redux ์ข…ํ•ฉํ€ด์ฆˆ๋ฅผ ํ’€๊ณ  ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฐœ๋…์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

โŒ˜ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…๋ฅ˜: React Context, Redux, MobX, recoil, zustand โ€ฆ

โŒ˜ Reducer๋Š” ์ด์ „ ์ƒํƒœ์™€ ๋™์ž‘์„ ๋ฐ›์•„ ์ƒˆ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜

โŒ˜ Action ๊ฐ์ฒด์˜ ๊ฐ’์„ ํ™•์ธํ•œ ํ›„, ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†์„ ๋• ๊ธฐ์กด ์ƒํƒœ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ด Good ๐Ÿ™†โ€โ™€๏ธ

โŒ˜ Redux์˜ Store ์„ค์ • ๋ฐฉ๋ฒ•

//index.js
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { reducer } from './reducer.js';

const store = createStore(reducer);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

โŒ˜ ๋งŒ์•ฝ ์—ฌ๋Ÿฌ ๊ฐœ์˜ Reducer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, Redux์˜ combineReducers ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜์˜ Reducer๋กœ ํ•ฉ์ฒด ๊ฐ€๋Šฅ

// Case: App.js์—์„œ reduce1์—์„œ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ

//reducer.js
import { combineReducers } from 'redux';

const reducer1 = (initialState = [1, 2, 3], action) => {
	...
};

const reducer2 = (initialState = {}, action) => {
	...
};

// combineReducers()๋กœ reducer1, reducer2๋ฅผ ํ•˜๋‚˜์˜ reducer๋กœ ํ•ฉ์ฒด
export const rootReducer = combineReducers({ reducer1, reducer2 });

// -------------------------------------------------------------

//index.js
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { rootReducer } from './reducer.js';

const store = createStore(rootReducer);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

// -------------------------------------------------------------

//App.js
import React from 'react';
import styled from 'styled-components';
import { useSelector } from 'react-redux';

...

export default function App() {
	// reducer2 ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด reducer1 -> reducer2๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋จ!
  const data = useSelector((state) => state.reducer1);
  console.log(data); // [1, 2, 3]
  return (
    ...
  );
}

โŒ˜ Reducer๊ฐ€ ์ฒ˜์Œ ํ˜ธ์ถœ๋  ๋•Œ, state๊ฐ’์€ undefined
๐Ÿ‘‰ย ๋”ฐ๋ผ์„œ state์˜ ์ดˆ๊นƒ๊ฐ’์„ ์ง€์ •ํ•ด์„œ Action ๋ฐœ์ƒ ์ „์— ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•จ
If not ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • โ†’ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

//reducer.js
// 1)
export const reducer = (initialState, action) => {
	if (typeof initialState === undefined) {
      initialState = 1;
    }
	...
};

// 2)
export const reducer = (initialState = 1, action) => {
	...
};


โœš [Github]

2๊ฐœ์˜ repository๋ฅผ ํ•˜๋‚˜์˜ repository๋กœ ํ•ฉ์น˜๊ธฐ

์ „์— ๊ตฌํ˜„ํ•œ CRUD-diary-toy_project๋ฅผ ๊ณ„์† refactoringํ•˜๋ฉด์„œ ๊ณ„์† update ํ•ด๋‚˜๊ฐ€๊ณ  ์‹ถ์–ด์„œ ๊ฐ๊ฐ์˜ client, server repository๋ฅผ ํ•˜๋‚˜์˜ repository๋กœ ํ•ฉ์น˜๊ณ  ์‹ถ์–ด์„œ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ณ  ์ ์šฉํ•ด๋ดค๋‹ค!

1. ํ•ฉ์ณ์„œ ์ƒˆ๋กญ๊ฒŒ ๊ด€๋ฆฌํ•  repository๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ํ›„, Local์—์„œ clone ๋ฐ›๊ธฐ
git clone https://github.com/Beanxx/React-Express-CRUD-Project.git

2. ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ํด๋”์—์„œ subtree๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด repository์— ์ถ”๊ฐ€ํ•  ๊ธฐ์กด repository๋ฅผ ๋”ํ•ด์คŒ
git subtree add --prefix=[์ƒˆ repositry์— ์ถ”๊ฐ€ํ•  ํด๋”๋ช…] [์ถ”๊ฐ€ํ•  ๊ธฐ์กด git repository ์ฃผ์†Œ] [๊ธฐ์กด repository branch name]

3. ์ƒˆ๋กœ์šด repository์— push
git push

์˜ค๋Š˜์€ ์–ด์ œ Redux ๊ณผ์ œ๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ์ผ์ฐ ๋๋‚œ ๋•์— ์—ฌ์œ ๋กญ๊ฒŒ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋‚  ูฉ( แ› )ูˆ
์–ด์ œ ๋„ˆ๋ฌด ํ”ผ๊ณคํ•ด์„œ ์›๋ž˜ ์ž๋˜ ์‹œ๊ฐ„๋ณด๋‹ค ์ผ์ฐ ์žค๋Š”๋ฐ๋„ ์˜ค์ „์— ๋„˜ ํ”ผ๊ณคํ–ˆ๋‹ค..
์˜ค๋Š˜์€ ์ €๋… ์•ฝ์†๋„ ์—†์œผ๋‹ˆ๊นŒ ์ง„์งœ์ง„์งœ ๊ณ„์† ๋ฏธ๋ฃจ์ง€ ๋ง๊ณ  ๊ณ„ํšํ–ˆ๋˜ ๊ฐ•์˜๋„ ๋‹ค ๋“ฃ์ž ์ ญ์•Œ ๐Ÿ™

โœ๏ธ Todo List
โ˜‘๏ธย TIL ์ž‘์„ฑ
โ˜‘๏ธย ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1_์ •์ˆ˜ ์ œ๊ณฑ๊ทผ ํŒ๋ณ„ ๋ฌธ์ œ ํ’€๊ธฐ
โ˜‘๏ธย ์ธํ”„๋Ÿฐ ๋ฆฌ์•กํŠธ ๊ฐ•์˜(Node.js, React.js ๊ธฐ์ดˆ ํŒŒํŠธ)๋“ฃ๊ธฐ
โ˜‘๏ธย Udemy React Section3 ๊ฐ•์˜ ๋“ฃ๊ธฐ
โ˜‘๏ธย Udemy ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ•์˜ ๋“ฃ๊ธฐ
โ˜‘๏ธย DeepDive ์ฑ… 1~3์žฅ ์ฝ๊ธฐ(~p.33)

profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€