๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

[์—๋Ÿฌ๋…ธํŠธ] defaultState for reducer handling [object Object] should be defined

JOTOKKI 2021. 3. 17. 11:29
728x90

๋ฆฌ๋•์Šค ์Šคํ„ฐ๋””์ค‘ handleAction์„ ์‚ฌ์šฉํ•˜๋˜ ์ค‘ ํ•ด๋‹น ์†Œ์Šค์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ

// ๋ฆฌ๋•์Šค ๋ชจ๋“ˆ
import { createAction, handleAction } from 'redux-actions'

const INCREASE = 'counter/INCREASE'
const DECREASE = 'counter/DECREASE'

export const increase = createAction(INCREASE)
export const decrease = createAction(DECREASE)

const initialState = 0

const counter = handleAction({
   [INCREASE]: state => state + 1,
   [DECREASE]: state => state - 1
}, initialState)

export default counter;

defaultState for reducer handling [object Object] should be defined

์—๋Ÿฌํ™”๋ฉด

 

์ด๋Ÿฌํ•œ ์—๋Ÿฌ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์ด์œ ๋Š” ์•ก์…˜์ด 2๊ฐœ ์ด์ƒ์ด๊ธฐ ๋•Œ๋ฌธ. 

2๊ฐœ ์ด์ƒ์˜ ์•ก์…˜์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด handleAction => handleActions๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด ์—๋Ÿฌ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค .

 

๊ตณ์ด ๊ตณ์ด handleAction์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค๋ฉด, ํ•˜๋‚˜์˜ ์•ก์…˜๋งŒ ์„ค์ •ํ•ด ์ฃผ๋ฉด๋œ๋‹ค. 

// ๋ฆฌ๋•์Šค ๋ชจ๋“ˆ
import { createAction, handleAction } from 'redux-actions'

const INCREASE = 'counter/INCREASE'

export const increase = createAction(INCREASE)

const initialState = 0

const counter = handleAction(
    INCREASE,
    state => state + 1,
    initialState
) 

export default counter;

๋ณดํ†ต์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋ฉด handleAction์€ ๊ฑฐ์˜ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์„๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿคจ

๋ฐ˜์‘ํ˜•