๐ŸคRedux - Part 2. React + Redux (3)

1. store ์—์„œ state ๋ฅผ ๋ฐ›์•„์˜ค๊ฒŒ ํ•˜๊ธฐ.

์–ด์ œ Vanilla JS + Redux ๋กœ TODO ์•ฑ์„ ๋งŒ๋“ค์–ด ๋ณด์•˜์„ ๋•Œ, store ์—์„œ ํ˜„์žฌ์˜ state ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

store.getState()

์ด์ œ Home.js ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฆฌํ„ดํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„์—์„œ ul ํƒœ๊ทธ ์•ˆ์—๋‹ค store ๋กœ๋ถ€ํ„ฐ state ๋ฅผ ๋ฐ›์•„์™€ ๋ Œ๋”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ ค๋ฉด ๋จผ์ € ํ˜„์žฌ ๋‚ด ์ปดํฌ๋„ŒํŠธ๋“ค์„ store ์— ์—ฐ๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

2. connect

๊ทธ๊ฒŒ ๋ฐ”๋กœ connect ๋ผ๋Š” ๊ฒƒ์ธ๋ฐ, ๋‚˜์˜ components ๋“ค์„ store ์— ์—ฐ๊ฒฐํ•˜๊ฒŒ ํ•ด ์ค€๋‹ค.

connect ๋Š” ๋‘๊ฐœ์˜ ์ธ์ž (state, dispatch) ๋ฅผ ๊ฐ€์ง„๋‹ค. ๋‘˜ ์ค‘์— ๊ณ ๋ฅผ ์ˆ˜ ์žˆ์–ด์„œ ์ด๋‹ค.

์—ฐ๊ฒฐ์„ ์œ„ํ•ด store ๋กœ๋ถ€ํ„ฐ state ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ํ•˜๋Š” (getCurrentState) ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„์€ โ€œmapStateToPropsโ€ ๋กœ ํ•˜๋Š”๊ฒŒ ๊ตญ๋ฃฐ์ด๋ผ ํ•œ๋‹ค.

const mapStateToProps = (state, ownProps) => {}

Redux state ๋กœ๋ถ€ํ„ฐ Component ์— props ๋กœ์จ ์ „๋‹ฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์˜ export default Home ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณ ์นœ๋‹ค.

export default connect(mapStateToProps)(Home)

์ด์ œ Home ์ปดํฌ๋„ŒํŠธ๋Š” store ์™€ ์—ฐ๊ฒฐ ๋˜์—ˆ๋‹ค.

import { connect } from 'react-redux'

Home.js ์ƒ์œ„์— import ํ•ด์˜ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž.

์ด์ œ store ์—์„œ ํ˜„์žฌ state ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” mapStateToProps ๋ฅผ ๋งˆ์ € ์ž‘์„ฑํ•ด๋ณธ๋‹ค.

3. mapStateToProps, Redux store ๋กœ๋ถ€ํ„ฐ ๋ญ”๊ฐ€๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ  ์‹ถ์„ ๋•Œ

store ์˜ reducer ์— ์ •์˜ํ•œ ์ดˆ๊ธฐ state ๋ฅผ ๋นˆ ๋ฐฐ์—ด์ด ์•„๋‹Œ state = [โ€œinitValโ€] ๋กœ ์ง€์ •ํ•ด ๋‘๊ณ 

์ฝ˜์†”๋กœ๊ทธ๋ฅผ ์ฐ์–ด ๋ณด์•˜๋‹ค.

const mapStateToProps = (state, ownProps) => {
  console.log(state, ownProps)
}

๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ๋Š”,

["initVal"] {history: {โ€ฆ}, location: {โ€ฆ}, match: {โ€ฆ}, staticContext: undefined}

mapStateToProps() in Connect(Home) must return a plain object. Instead received undefined.

์ด๋Ÿฐ ๊ฒŒ ๋œฌ๋‹ค.

์ฒซ ์ค„์—์„œ ๋ณด๋ฉด firstArgument ๋Š” redux store ์—์„œ ์˜จ ๋””ํดํŠธ state ์ด๋‹ค. (initVal)

๋‘ ๋ฒˆ์งธ Argument ๋Š” ๋‚ด ์ปดํฌ๋„ŒํŠธ์˜ props ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

(App.js ์˜ react-router ์—์„œ ๋‚ด๋ ค์ค€ props)

๊ทธ๋ ‡๋‹ค๋Š” ๋œป์€ Home Component ์—์„œ props ๋กœ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ,

์•„๋ž˜์™€ ๊ฐ™์ด props ๋ฅผ Home ์ปดํฌ๋„ŒํŠธ์— ์ธ์ž๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ณ ,

console.log ๋ฅผ ์ฐ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋ณด์•˜๋‹ค.

const Home = (props) => {
  console.log(props);
  // ! ์—ฌ๊ธฐ์„œ props ๋กœ ๋ฐ›์•„์™”๋‹ค. App.js ์˜ react-router ๋กœ๋ถ€ํ„ฐ!
  ... ์ดํ•˜ ์ค‘๋žต ...

๋งจ ์•„๋ž˜์˜ must return a plain object. ๋Š” ์•„์ง ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜์ง€ ์•Š์•„ ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜์ด๋‹ค.

๊ทธ๋ž˜์„œ,

const mapStateToProps = (state, ownProps) => {
  return { happy: true }
}

๋ฅผ ํ•ด๋ณด๋‹ˆ, ์œ„์˜ ์ฝ˜์†”๋กœ๊ทธ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ props ๋ฅผ ์ฐ๋Š”๋‹ค.

{history: {โ€ฆ}, location: {โ€ฆ}, match: {โ€ฆ}, staticContext: undefined, happy: true, โ€ฆ}

{ happy: true } ๊ฐ€ props ๋กœ ์ถ”๊ฐ€ ๋˜์–ด ์žˆ๋‹ค.

why? connect() ๋Š” Home ์œผ๋กœ ๋ณด๋‚ด๋Š” props ์— ์ถ”๊ฐ€ ๋  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฌด์—‡์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•˜๋“  ๋‚˜์˜ Home component ์˜ props ์— ์ถ”๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

์ด์ œ store ์˜ state ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•ด mapStateToProps ๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ–ˆ๋‹ค.

const getCurrentState = state => {
  return { toDos: state } // ์—ฌ๊ธฐ์„œ ownProps ๋Š” ํ•„์š”์—†์–ด์„œ ์ง€์› ๋‹ค.
}

์ด์ œ, store ์˜ state ๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ์žˆ๋‹ค.. ์™€์•„..


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook