TIL_20.08.26 ๐Ÿƒ๐Ÿฝโ€โ™‚๏ธ๐Ÿƒ๐Ÿฝโ€โ™‚๏ธ

Doum Kimยท2020๋…„ 8์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
35/71
post-thumbnail

Codestates immersive course


Redux ์Šคํ”„๋ฆฐํŠธ

์˜ค๋Š˜์€ ์–ด์ œ ๋ฏธ๋ฆฌ ๊ณผ์ œ๋ฅผ ์™„์„ฑํ•ด ๋†“์€ ์ƒํƒœ๋ผ ํ˜ผ์ž์„œ ๋ณต์Šต์„ ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์กŒ๋‹ค.

ํŠนํžˆ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ connect๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ์—์„œ store์— ์ ‘๊ทผํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ react-redux์—์„œ ์ง€์›ํ•˜๋Š” useSelector, useDispatch๋ฅผ ์ด์šฉํ•ด์„œ

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด ๋ดค๋‹ค.

์ผ๋‹จ ๊ทธ๋ž˜๋„ connect ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ •๋ฆฌํ•ด๋ณด๋ฉด

connect(mapStateToProps,mapDispatchToProps)(Component) ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

mapStateToProps

As the first argument passed in to connect, mapStateToProps is used for selecting the part of the data from the store that the connected component needs. Itโ€™s frequently referred to as just mapState for short.
It is called every time the store state changes.
It receives the entire store state, and should return an object of data this component needs.
-์ถœ์ฒ˜ ๋ฆฌ๋•์Šค ๊ณต์‹ ๋ฌธ์„œ

mapDispatchToProps

As the second argument passed in to connect, mapDispatchToProps is used for dispatching actions to the store.
dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change.
With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions:
By default, a connected component receives props.dispatch and can dispatch actions itself.
connect can accept an argument called mapDispatchToProps, which lets you create functions that dispatch when called, and pass those functions as props to your component.
The mapDispatchToProps functions are normally referred to as mapDispatch for short, but the actual variable name used can be whatever you want.

๋ฌดํŠผ ๋‚˜๋Š” container ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  connect๋ฅผ ์ด์šฉํ•ด์„œ ์ปดํฌ๋„ŒํŠธ์—์„œ store์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์ฃผ๊ฒŒ ํ•˜๋Š”๊ฒŒ ๊ท€์ฐฎ์œผ๋‹ˆ useSelector์™€ useDispatch๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

Flux ํŒจํ„ด์„ ์ƒ๊ฐํ•˜๋ฉฐ action -> dispatch -> store -> view

์•ก์…˜ ์ƒ์„ฑ ํ•จ์ˆ˜

const INCREMENT = "INCREMENT";
const DECREMENT = "DECREMENT"; 

export const increment = () => ({
  type: INCREMENT
});

export const decrement = () => ({
  type: DECREMENT
});

๋ฆฌ๋“€์„œ ํ•จ์ˆ˜ ์ƒ์„ฑ

import * as types from "../actions/actionTypes";

const initialState = {
  count: 0
};

const counter = (state = initialState, action) => {
  switch (action.type) {
    case types.INCREMENT:
      return {
        ...state,
        count: state.count + 1
      };
    case types.DECREMENT:
      return {
        ...state,
        count: state.count - 1
      };
    default:
      return state;
  }
};

export default counter;

store ์ƒ์„ฑ

import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
import { Provider } from "react-redux";
import { createStore } from "redux";
import reducers from "./reducers";

const store = createStore(reducers);

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Provider store={store}>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </Provider>,
  rootElement
);

Counter.js

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "../actions";

const Counter = () => {
  let count = useSelector((state) => state.counter.count);
  const dispatch = useDispatch();
  return (
    <>
      <h1>{count}</h1>
      <button onClick={() => dispatch(increment())}>+1</button>
      <button onClick={() => dispatch(decrement())}>-1</button>
    </>
  );
};

export default Counter;

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