Redux

๊น€๋„๋„›ยท2021๋…„ 11์›” 4์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
5/5

๐Ÿ”ฅ Redux

๐ŸŒˆ Redux ?

Redux๋Š” JavaScript ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ data-state ์™€ UI-state ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๋„๊ตฌ
์ƒํƒœ์  ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๊ฐ€ ์‹œ๊ฐ„์ด ํ๋ฆ„์— ๋”ฐ๋ผ ๋ณต์žกํ•ด์งˆ์ˆ˜๋„ ์žˆ๋Š” ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ (Single Page Application) ์—์„œ ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , Redux๋Š” React ์™ธ์—๋„, jQuery ํ˜น์€ Angular ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋„ ์‚ฌ์šฉ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

React ์—์„œ ๋ฐ์ดํ„ฐํ๋ฆ„์€ ๋‹จ์ผ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํ๋ฅธ๋‹ค
parent-child ๊ด€๊ณ„๋ฅผ ํ†ตํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ต๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ๋‹จ์ :
์ปดํฌ๋„ŒํŠธ ๊ฐฏ์ˆ˜ ์ฆ๊ฐ€์˜ ๊ฒฝ์šฐ ๋˜๋Š”
์ปดํฌ๋„ŒํŠธ๋“ค์˜ ๊ด€๊ณ„๊ฐ€ parent-child ๊ด€๊ณ„ ์•„๋‹ ๊ฒฝ์šฐ
==> ๋ณต์žกํ•ด์ง„๋‹ค
๋นจ๊ฐ„์ƒ‰์ด ํŒŒ๋ž€์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋ ค๋ฉด ๋ณต์žกํ•ด์ง„๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ์•ˆ์˜ ํ•˜๋‚˜: MVC ๋””์ž์ธ ํŒจํ„ด ์ค‘ FLUX ํŒจํ„ด

โšก๏ธ MVC ๋””์ž์ธ ํŒจํ„ด

MVC ํŒจํ„ด์€ Controller, Model, View ์ด 3๊ฐ€์ง€ ๊ฐœ๋…์œผ๋กœ ์ด๋ค„์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋– ํ•œ Action ์ด ์ž…๋ ฅ๋˜๋ฉด, Controller์€ Model์ด ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉฐ, ์ด ๋ณ€ํ™”๋Š” View ์— ๋ฐ˜์˜๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, View์—์„œ Model์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿค” Model๊ณผ View๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค๋ฉด ?


๋ฌดํ•œ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„,, ์–ด๋””์„œ ์ž˜๋ชป๋œ๊ฑด์ง€ ์ฐพ๊ธฐ๋„ ํž˜๋“ค๋‹ค
์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ FLUX ๋ผ๋Š” ๋””์ž์ธ ํŒจํ„ด์ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.

โญ๏ธ FLUX ํŒจํ„ด


์‹œ์Šคํ…œ์—์„œ ์–ด๋– ํ•œ Action ์„ ๋ฐ›์•˜์„ ๋•Œ, Dispatcher๊ฐ€ ๋ฐ›์€ Action๋“ค์„ ํ†ต์ œํ•˜์—ฌ Store์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€๋™๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด View ์— ๋ฆฌ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ , View์—์„œ Dispatcher๋กœ Action์„ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์ฃ 

Dispatcher์€ ์ž‘์—…์ด ์ค‘์ฒฉ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ์ฆ‰, ์–ด๋–ค Action์ด Dispatcher๋ฅผ ํ†ตํ•˜์—ฌ Store์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ทธ ์ž‘์—…์ด ๋๋‚  ๋•Œ ๊นŒ์ง€ ๋‹ค๋ฅธ Action๋“ค์„ ๋Œ€๊ธฐ์‹œํ‚ต๋‹ˆ๋‹ค.

๐Ÿš€ Redux

Redux๋Š”, ์œ„์—์„œ ์„ค๋ช…๋œ Flux ์•„ํ‚คํ…์ณ๋ฅผ ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋ฅผ ๋‹ค์Œ๊ณผ๊ฐ™์ด ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด, store์—์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๊ณ , ์ปดํฌ๋„ŒํŠธ๋ผ๋ฆฌ๋Š” ์ง์ ‘ ๊ต๋ฅ˜ํ•˜์ง€ ์•Š๊ณ  store ์ค‘๊ฐ„์ž๋ฅผ ํ†ตํ•˜์—ฌ ๊ต๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๋นจ๊ฐ„ ํ™”์‚ดํ‘œ๋Š” dispatch ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ store์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š”๊ฒƒ์„ ๊ฐ€๋ฅดํ‚ค๊ณ , ์ฃผํ™ฉ์ƒ‰ ํ™”์‚ดํ‘œ๋Š” subscribe๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ store์— ์žˆ๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ์˜ ๋ณ€๋™์„ ์ฃผ์˜ํ•˜๊ณ ์žˆ๋‹ค๊ฐ€ ๋ณ€๋™์ด ์žˆ์„์‹œ ๋ฐ”๋กœ ๋ฐ˜์˜์‹œํ‚ค๋Š”๊ฒƒ์„ ๊ฐ€๋ฅดํ‚ต๋‹ˆ๋‹ค.

๐Ÿ“Œ Redux ์˜ 3๊ฐ€์ง€ ์›์น™

1. Single Source of Truth (๋‹จ ํ•˜๋‚˜์˜ ์ง„์‹ค์˜ ๊ทผ์›)

Redux๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ state๋ฅผ ์œ„ํ•ด ๋‹จ ํ•œ๊ฐœ์˜ store ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
Flux ์™€์˜ ์ฃผ์š” ์ฐจ์ด ์ž…๋‹ˆ๋‹ค. Flux ์—์„œ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ store ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.)

2. State is read-only (State๋Š” ์ฝ๊ธฐ์ „์šฉ์ด๋‹ค)

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ state๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝ ํ•  ์ˆ˜๋Š” ์—†๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
state ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, action ์ด dispatch ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.(dispatch ๋Š” โ€˜๋ณด๋‚ธ๋‹คโ€™๋Š” ๋œป ์ž…๋‹ˆ๋‹ค)
action ์€ ์–ด๋–ค ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚˜์•ผ ํ•  ์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ฐ์ฒด

3. Changes are made with Pure Functions (๋ณ€ํ™”๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ ธ์•ผ ํ•œ๋‹ค)

๋ฐ›์•„์˜จ action ๊ฐ์ฒด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ Reducer ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.
action์€ ์–ด๋–ค ๋ณ€ํ™”๋ฅผ ์ผ์–ด๋‚˜์•ผ ํ•  ์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ฐ์ฒด๋ผ๋ฉด, Reducer ๋Š” ๊ทธ ์ •๋ณด๋ฅผ ๋ฐ›๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€์ง€ ์ •์˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Reducer ๋Š” โ€˜์ˆœ์ˆ˜ ํ•จ์ˆ˜โ€™ ๋กœ๋งŒ ์ž‘์„ฑ

์ˆœ์ˆ˜ํ•จ์ˆ˜์—ฌ์•ผํ•˜๋Š” ์ด์œ :

Redux๋Š” ๋‘ ๊ฐ์ฒด(prevState,newState)์˜ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ด์ „ ๊ฐ์ฒด๊ฐ€ ์ƒˆ ๊ฐ์ฒด์™€ ๋™์ผํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ๋‹จ์ˆœ ์ฒดํฌํ•œ๋‹ค(shallowCompare,===). ๋งŒ์•ฝ ๋ฆฌ๋“€์„œ ๋‚ด๋ถ€์—์„œ ์ด์ „ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์ƒˆ ์ƒํƒœ ์™€ ์ด์ „ ์ƒํƒœ๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฆฌ๋•์Šค๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋ฐ”๋€ ์†์„ฑ๋งŒ ๋น„๊ตํ•˜๋ฉด ๋˜์ง€ ์™œ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฑธ๊นŒ? ๋ผ๋Š” ์˜๋ฌธ์ด ์ƒ๊ธด๋‹ค. ๊ทธ ์ด์œ ๋Š” ์„ฑ๋Šฅ๊ณผ ๋ณต์žก๋„์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์œ„์˜ ์˜ˆ์ œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฉ์น˜๊ฐ€ ํฐ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•ด์•ผ ๋˜๋Š” ์ƒํ™ฉ์ด๋ฉด ๊ฐ์ฒด์˜ ์ฃผ์†Œ ๋น„๊ต๋ผ๋ฉด O(1) ๋งŒํผ ๋น„๊ต๋ฅผ ํ•˜๋ฉด ๋˜์ง€๋งŒ, ์†์„ฑ์„ ๋น„๊ตํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด O(n)๋งŒํผ ๋น„๊ต๋ฅผ ํ•ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.

๐Ÿ”ฅ React-Redux์—์„œ ๋‚˜์˜ค๋Š” ๊ฐœ๋…

react-redux๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋Œ€๋ถ€๋ถ„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๐Ÿ’ก Provider

  • Provider๋Š” connect() ํ•จ์ˆ˜์— ํฌ์žฅ ๋œ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ Redux Store๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์ „์ฒด ์•ฑ์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ Provider๋ฅผ ๋ Œ๋”๋งํ•œ๋‹ค.
  • Provider์˜ Props๋กœ๋Š” store๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ Redux Store์ด๋‹ค.
import React from "react";
import ReactDOM from "react-dom";
// Provider๋Š” react-redux ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
import { Provider } from "react-redux";
// createStore๋Š” redux์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
import { createStore } from "redux";
import App from "./components/App";
import reducers from "./reducers";
ReactDOM.render(
// connect()ํ•จ์ˆ˜์— ํฌํ•จ๋œ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ redux store๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ์œ„ํ•ด,
// Provider๋Š” ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ๋ Œ๋”๋ง ๋œ๋‹ค
// provider์˜ props๋กœ store๊ฐ€ ์žˆ์œผ๋ฉฐ redux store๋ฅผ ํ• ๋‹นํ•œ๋‹ค.
  <Provider store={createStore(reducers)}>
    <App />
  </Provider>,
  document.querySelector("#root")
);

๐Ÿ’ก connect()

  • connect() ํ•จ์ˆ˜๋Š” React ์ปดํฌ๋„ŒํŠธ๋ฅผ Redux Store์— ์—ฐ๊ฒฐํ•œ๋‹ค.
  • connect()๋กœ ์—ฐ๊ฒฐํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(state)๋ฅผ store์—์„œ ๋ฐ›์•„์˜จ๋‹ค.
  • connect()๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฒคํŠธ๋กœ ํ˜ธ์ถœ ๋œ action์„ store๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.(dispatch)
1. connect() ํ•จ์ˆ˜๋ฅผ 'react-redux' ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
2. 'export default connect()(์˜ˆ์‹œ์ปดํฌ๋„ŒํŠธ)' ํ˜•ํƒœ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ connect()ํ•จ์ˆ˜๋กœ ์—ฐ๊ฒฐํ•œ๋‹ค.
3. (connect()์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ mapStateToProps, ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” reducer์— ์ „๋‹ฌํ•  action์„ ๋„ฃ๋Š”๋‹ค.)

๋ณด๊ธฐ์—” ์–ด์ƒ‰ํ•˜์ง€๋งŒ,
function connect()()๋Š” ์œ ํšจํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์ด๋‹ค.

function connect(){
  return function(){
    return '์•ˆ๋…•'
  }
}
connect()() // '์•ˆ๋…•'

๐Ÿ’ก mapStateToProps

  • connect() ํ•จ์ˆ˜์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค.
  • store ๋‚ด๋ถ€์˜ ๋ชจ๋“  state๊ฐ’์„ ๋ฐ›์œผ๋ฉฐ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ์—ฐ๊ฒฐ๋œ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(state)๋ฅผ ์„ ํƒํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  • store์˜ ์ƒํƒœ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋œ๋‹ค.
// ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ์ฝ”๋“œ์˜ ์™ธ๋ถ€์—์„œ mapStateToProps ์‚ฌ์šฉ(store ์—์„œ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ)
const mapStateToProps = (state) => {
  return {
    //๋ฐ›์•„์˜ฌ๋•Œ ์“ธ state์˜ ์ด๋ฆ„' : state.'reducer์—์„œ ์ •ํ•œ ๋ฐ์ดํ„ฐ ์ด๋ฆ„' 
    needData : state.needData
  }
}

๐Ÿ’ฌ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„

import React, {Commponent} from 'react'

์—ฌ๊ธฐ์„œ, ์™œ React๋Š” {}๊ฐ€ ์—†๊ณ  ์™œ Component ๋ถ€๋ถ„์€ {}๊ฐ€ ์žˆ์„๊นŒ?

import React from 'react'
  • ์ด๊ฒƒ์€ default import ๋ฅผ ๋งํ•œ๋‹ค.
  • ์šฐ๋ฆฌ๊ฐ€ ํ•จ์ˆ˜ ํ˜น์€ ํด๋ž˜์Šค ์•ž๋‹จ์— ์„ค์ •์„ ํ•  ๋–„, 'export default' ํ˜น์€ 'export' ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์“ด ๊ธฐ์–ต์ด ์žˆ์„ ํ…๋ฐ, 'export default' ๋ฅผ ์“ธ ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์˜ค์ง ํ•œ๊ฐœ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
import { Component } from 'react'
  • ์œ„์™€ ๋‹ค๋ฅด๊ฒŒ ์ด๊ฒƒ์€ member import(named import) ๋ผ๊ณ  ๋ถ€๋ฅด๊ณ , member import๋Š” export ๋  ๋•Œ, export ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์—ฌ๋Ÿฌ๊ฐœ์˜ member export๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
/config.js

const a = 0;
const b = 1
export { a };
export const c = 2;
export default b;
import f, {a, c as two} from '/config';
console.log(f, a, two)	// 1, 0, 2

export default๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์ค‘๊ด„ํ˜ธ {} ์—†์ด ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ณ€์ˆ˜๋ช…์ด ๋‹ฌ๋ผ๋„ ๋œ๋‹ค

export default b = f = 1

๋‹จ์ง€ export๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋“ค์€ ์ค‘๊ด„ํ˜ธ {}๋กœ ๋ฐ›์•„์™€์•ผ ํ•˜๊ณ , ๋ณ€์ˆ˜๋ช…์ด ๋™์ผํ•ด์•ผ ํ•œ๋‹ค

๋ณ€์ˆ˜๋ช…์„ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” c as two ์ฒ˜๋Ÿผ as๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค

์ถœ์ฒ˜

https://velopert.com/1225
https://velopert.com/3401
https://velopert.com/3486
https://ideveloper2.tistory.com/159
https://velog.io/@velopert/Redux-3-%EB%A6%AC%EB%8D%95%EC%8A%A4%EB%A5%BC-%EB%A6%AC%EC%95%A1%ED%8A%B8%EC%99%80-%ED%95%A8%EA%BB%98-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-nvjltahf5e
https://stackoverflow.com/questions/36084515/how-does-shallow-compare-work-in-react
https://velog.io/@mokyoungg/Redux-react-redux-react-redux
https://redux.js.org/faq/immutable-data#what-are-the-benefits-of-immutability

profile
๋ฐฑ์•ค๋“œ๊ฐœ๋ฐœ ๊ธฐ๋ก

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