๊ฐ์๊ธฐ์์:
const todoBlog = combineReducers({
blogTypeVisibilityFilter,
blogs
})
blogTypeVisibilityFilter์์:
const blogTypeVisibilityFilter = (state, action)=>{
switch (action.type) {
case 'BLOG_TYPE_VISIBILITY_FILTER':
return action.filter
default:
return state
}
}
export default blogTypeVisibilityFilter;
๋ธ๋ก๊ทธ์์:
const blogs = (state,action)=>{
return state
}
createStore์์:
const initialState = {
blogTypeVisibilityFilter:'SHOW_ALL_BLOG',
blogs:data.data,
}
const store = createStore(reducer,initialState,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
๊ทธ๋ฐ ๋ค์ ์๋ชป ํ์๋ฉ๋๋ค.
์ด๊ธฐํํ๋ ๋์ ๊ฐ์๊ธฐ "blogTypeVisibilityFilter"๊ฐ ์ ์๋์ง ์์์ ๋ฐํํ์ต๋๋ค. ๊ฐ์๊ธฐ์ ์ ๋ฌ๋ ์ํ๊ฐ ์ ์๋์ง ์์ ๊ฒฝ์ฐ ์ด๊ธฐ ์ํ๋ฅผ ๋ช ์์ ์ผ๋ก ๋ฐํํด์ผ ํฉ๋๋ค. ์ด๊ธฐ ์ํ๋ ์ ์๋์ง ์์ ์ ์์ต๋๋ค. ์ด ๊ฐ์๊ธฐ์ ๋ํ ๊ฐ์ ์ค์ ํ์ง ์์ผ๋ ค๋ฉด undefined ๋์ null์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ง๋ง ๋ด๊ฐ ๋ฐ๋ ๋
const todoBlog = combineReducers({
blogTypeVisibilityFilter,
blogs
})
์๊ฒ
const todoBlog = (state={},action)=>{
return{
blogTypeVisibilityFilter:blogTypeVisibilityFilter(state.blogTypeVisibilityFilter,action),
blogs:blogs(state.blogs,action)
}
}
๊ฐ์๊ธฐ์์๋ ์ค๋ฅ ์์ด ์ ์คํ๋ฉ๋๋ค.
์ ๋๋ CombineReducers๋ฅผ ์ฌ์ฉํฉ๋๊น? ๊ทธ๊ฒ์ ์๋ชป๋ฉ๋๊น?
์ด๊ฒ์ ์ง์ ์์คํ ์ด ์๋ ๋ฒ๊ทธ ์ถ์ ๊ธฐ์ ๋๋ค. ์ฌ์ฉ ์ง๋ฌธ์ ์คํ ์ค๋ฒํ๋ก ๋๋ Reactiflux๋ฅผ ์ฌ์ฉํ์ญ์์ค. ๊ฐ์ฌ ํด์!
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋จ๊ณ:
{ foo, bar }
.simpleReducer = state => state
์ ๊ฐ์ ๊ฐ๋จํ ๊ฐ์๊ธฐ๋ฅผ ๋ง๋ญ๋๋ค.reducers = combineReducers({ foo: simpleReducer, bar: simpleReducer })
์์ ๊ฐ์๊ธฐ๋ฅผ ๊ฒฐํฉํฉ๋๋ค.createState(reducers, { foo: Obj1, bar: Obj2 })
์ด๊ธฐ๊ฐ์ผ๋ก ์ํ๋ฅผ ์์ฑํฉ๋๋ค.Reducer "foo" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state
์ด์ ๋ ์ด๊ธฐ ์ํ ๋ถ๋ถ์ ์ป๊ธฐ ์ํด ์ ์๋์ง ์์ ์ํ ๋ก ์คํ ํ๋ assertReducerShape
์
๋๋ค. ๋ด ๊ฐ๋จํ ๊ฐ์๊ธฐ๋ ์ด ๊ฒฝ์ฐ ์ ์๋์ง ์์ ์ธ์๋ฅผ ๋ฐํํฉ๋๋ค. createState
ํธ์ถ์ Obj1
๊ฐ foo-part์ ์ด๊ธฐ ์ํ๋ก ๊ฐ์ฃผ๋์ง ์๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
@timdorr ๋ฌธ์ ๋ฅผ ๋ค์ ์ด ์ ์์ต๋๊น?
ํด๊ฒฐ ๋ฐฉ๋ฒ์ simpleReducer = (state = null) => state
๋ฅผ ๋ง๋๋ ๊ฒ์
๋๋ค. ํ์ง๋ง ๊ด์ฐฎ์?
combineReducers
ํ๋ฉด ๊ฐ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์๋ ํด๋น ์ํ ์ฌ๋ผ์ด์ค๋ฅผ "์์ "ํด์ผ ํฉ๋๋ค. ์ฆ, ํ์ฌ ์ฌ๋ผ์ด์ค๊ฐ ์ ์๋์ง ์์ ๊ฒฝ์ฐ ์ด๊ธฐ ๊ฐ์ ์ ๊ณตํ๊ณ , ๊ทธ๋ ๊ฒ ํ์ง ์๋ ๊ฒฝ์ฐ combineReducers
๊ฐ ์๋ ค์ค๋๋ค.
'์ด๊ธฐ ์ํ'์๋ ์ฌ๋ฌ ๊ฐ์ง ์๋ฏธ๊ฐ ์์ต๋๋ค. reducer์ ๊ธฐ๋ณธ ๋ฐํ ๊ฐ๊ณผ createStore์ ๋ ๋ฒ์งธ ์ธ์์ ๋๋ค. ๋ฌธ์ ๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. CombineReducers๊ฐ initialState ๋ฐ createState์ ๋ํ ๋ฆฌ๋์๋ฅผ ํ์ธํ์ง ์๋ ์ด์ ๋ฅผ ์ค๋ช ํ ์ ์์ต๋๊น?
@Vittly : combineReducers
๋ ๊ณ ์์ ์ผ๋ก ์๊ฒฌ์ด ๋ถ๋ถํ ๋ฐ๋ฉด createStore
๋ ๊ทธ๋ ์ง ์์ต๋๋ค. createStore
๋ ์ฌ๋ฌ๋ถ์ด ๋ถ์ฌํ ๋ฃจํธ ๋ฆฌ๋์ ํจ์๋ฅผ ๋จ์ํ ํธ์ถํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํฉ๋๋ค. combineReducers
๋ ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ํ๊ฐ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋์ด์ผ ํ๊ณ ๊ฒฐํฉ๋ ๋ฆฌ๋์๊ฐ ์ด๋ป๊ฒ ์๋ํด์ผ ํ๋์ง์ ๋ํ ํน์ ๊ฐ์ ์ธํธ๋ฅผ ๊ตฌ๋งคํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
combineReducers
์ด(๊ฐ) ๋
๋จ์ ์ด๋ฉฐ ์ด๋ค ์๊ฒฌ์ ๊ฐ์ง๊ณ ์๋์ง์ ๋ํ ์ญ์ฌ์ ์ธ๋ถ ์ฌํญ์ ๋ํด ์ค๋ช
ํ๋ #191 ๋ฐ #1189๋ฅผ ์ฝ๊ณ ์ถ์ ์๋ ์์ต๋๋ค.
๋ฐ๋ผ์ ๋ฌธ์ ๋ (๊ฐ๋จํ) "๋ฆฌ๋์๋ฅผ ๊ฒฐํฉํ๋ ๊ฒฝ์ฐ ์ ์ฅ์ ํธ๋ฆฌ๋ ๋ถํ ํ๊ณ ๋ฌด์ธ๊ฐ๋ฅผ ๋์น๊ฑฐ๋ createStore 2nd ์ธ์์์ ์๋์ ์ผ๋ก ์ ์ฅ์์ ์ผ๋ถ๋ง ๋ฏธ๋ฆฌ ๋ก๋ํ ์ ์์ต๋๋ค. ์ ์ฅ์ ํธ๋ฆฌ๋ฅผ ๋ณด๋ค ์ผ๊ด์ฑ ์๊ฒ ์ฌ์ฉํ๋ ค๋ฉด assertReducerShape๋ฅผ ์ฌ์ฉํ์ญ์์ค." ์ข์์, ์ฌํ ๊ฐ์ฌํฉ๋๋ค
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๊ทธ๋์ createStore
ํ๊ณ ์ด๊ธฐ ์ํ๋ฅผ ์ ๋ฌํฉ๋๋ค. ํ์ง๋ง reduct.js
ํ์ผ์์ assertReducerShape
redux ํจ์๋ undefined
์ํ๋ฅผ ์ ๋ฌํ ๋ ๋ด ๊ฐ์๊ธฐ๋ฅผ ํ์ธํฉ๋๋ค.
IMHO ์ด๊ฒ์ ๋ฒ๊ทธ์ ๋๋ค.
@JoseFMP : ๋๋ ๋น์ ์ด ๋ณด๊ณ ์๋ ๋ชจ๋ ๊ฒ์ด ๋ฒ๊ทธ๊ฐ
ํ์ ํ๋. Stackoverflow์์ ๋ฌธ์ ๊ฐ ๋๋ ๋ฌธ์ ๋ฅผ ์ ์ถํ์ต๋๋ค.
https://stackoverflow.com/questions/53018766/why-redux-reducer-getting-undefined-instead-of-the-initial-state
๋ฌธ์ ๋ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ๊ทธ๋์ ์คํ ์ด๋ฅผ ์์ฑํ ๋ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋ค๋ฉด ์ redux๊ฐ undefined
์ํ๋ก ๋ฆฌ๋์๋ฅผ ํ๊ฐํ๋ ค๊ณ ํ ๊น์? ๊ทธ๋ฌ๋ฉด ์ค์ ๋ก ๋ชจ๋ ๊ฐ์๊ธฐ๊ฐ ์ ์ํ undefined
๋ฅผ ๋ฐํํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ธฐ ์ํ๋ฅผ ์ ๋ฌํ๊ธฐ ๋๋ฌธ์ ๋์ผ์ค์
๋๋ค.
๊ทธ๋์ ์ด๊ฒ์ ์คํจํ๊ณ ์์ต๋๋ค:
import { combineReducers, createStore } from 'redux';
const configReducer = (config: any, action: any): any =>{
return config;
}
const customData = (customData: any, action: any): any => {
return customData;
}
const reducers = combineReducers({config: configReducer, customData: customDataReducer})
const defaultConfig = "cool config";
const data = "yieaah some data";
var initialState = {config: defaultConfig, customData: data};
const store = createStore(reducers, initialState) // at this point redux calls all the reducers with 'undefined' state. Why?
@JoseFMP :
์, ๋ฌธ์ ๊ฐ ๋ญ์ง ์ ๊ฒ ๊ฐ์์. ์ด๊ฒ์ combineReducers()
์๋ ๋ฐฉ์์ ๋ฐ๋ผ ๋ค๋ฆ
๋๋ค.
combineReducers
๋ ๋น์ ์ด ์ ๊ณตํ๋ ๋ชจ๋ "์ฌ๋ผ์ด์ค ๋ฆฌ๋์"๊ฐ ๋ช ๊ฐ์ง ๊ท์น์ ๋ฐ๋ฅผ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค. ํนํ, ๊ฐ์๊ธฐ๊ฐ state === undefined
๋ก ํธ์ถ๋๋ฉด _if_ ์ ์ ํ ๊ธฐ๋ณธ๊ฐ์ ๋ฐํํ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค. ์ด๋ฅผ ํ์ธํ๊ธฐ ์ํด combineReducers()
๋ ์ค์ ๋ก (undefined, {type : "SOME_RANDOMIZED_ACTION_TYPE"})
๋ก ๊ฐ์๊ธฐ๋ฅผ ํธ์ถํ์ฌ ์ ์๋์ง ์์ ๊ฐ์ ๋ฐํํ๋์ง "์ค์ " ๊ฐ์ ๋ฐํํ๋์ง ํ์ธํฉ๋๋ค.
๊ฐ์๊ธฐ๋ ํ์ฌ ์ ๋ฌ๋ ๊ฒ์ ๋ฐํํ๋ ๊ฒ ์ธ์๋ ์๋ฌด ๊ฒ๋ ํ์ง ์์ต๋๋ค. ์ฆ, state
๊ฐ ์ ์๋์ง ์์ ๊ฒฝ์ฐ _return_ undefined๊ฐ ๋ฉ๋๋ค. ๋ฐ๋ผ์ combineReducers()
๋ ์์ํ ๊ฒฐ๊ณผ๋ฅผ ๊นจ๊ณ ์์์ ์๋ ค์ค๋๋ค.
์ ์ธ์ configReducer = (config = {}, action)
๋ฑ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๊ฒฝ๊ณ ๊ฐ ์์ ๋ฉ๋๋ค.
๋ค์ ๋งํ์ง๋ง, ์ด๊ฒ์ ๋ฒ๊ทธ๊ฐ _์๋๋๋ค_. ์ด๊ฒ์ ํ๋ ๊ฒ์ฆ์ ๋๋ค.
Hej @markerikson ์๋ต
๋ช ๊ฐ์ง ์๊ฒฌ:
1) ์๋์, combineReducers
๊ตญํ๋์ง ์์ต๋๋ค. combineReducers
์ฌ์ฉํ์ง ์๊ณ ๋๋ง์ ๋ฃจํธ ๋ฆฌ๋์๋ฅผ ๊ตฌํํด๋ ๊ฐ์ ์ผ์ด ๋ฐ์ํฉ๋๋ค.
2) ์ฌ๊ธฐ์ ๋ถ์ผ์น๋ Redux ๋ฌธ์์์ ๋ฆฌ๋์๊ฐ ์ ์ ์๊ฑฐ๋ ์์์น ๋ชปํ ์ํ๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ ์ํ๋ฅผ ์์ ํ์ง ์๊ณ ์ธ์๋ก ์ ๊ณต๋ ๊ฐ์ ๋ฐํํด์ผ ํ๋ค๊ณ ์ธ๊ธ๋์ด ์๋ค๋ ๊ฒ์
๋๋ค. ์ด ๋ฐ์ผ๋ฉด IE๋ ... undefined
๊ทธ ๋ค์์ ๋ฐํํด์ผํฉ๋๋ค undefined
. ๊ทธ๋ฌ๋ ๋ ๋ค๋ฅธ ๊ท์น์ ๋ฐ๋ฅด๋ฉด ๋ฆฌ๋์๋ undefined
๋ฐํํ์ง ์์์ผ ํ๋ฏ๋ก ํ์ฌ redux ๋ฌธ์์ ์๋ ๊ฒ์ฒ๋ผ ์ด ๋ ๊ท์น์ด ์ผ์นํ์ง ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตฌํ๊ณผ ์ผ์นํ์ง ์์ต๋๋ค.
@JoseFMP : ์์ ๋งํ๋ฏ์ด ๋ฌธ์ ๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช ํ๋ CodeSandbox๋ฅผ ์ ๊ณตํ ์ ์๊ณ ์์๋๋ ์ผ๊ณผ ์ค์ ๋ก ์ผ์ด๋๊ณ ์๋ ์ผ์ ์ ํํ ์ง์ ํ๋ ์ฃผ์์ ์ ๊ณตํ ์ ์๋ค๋ฉด ์ดํด๋ณผ ์ ์์ต๋๋ค. (๋ํ ์ผ๊ด์ฑ์ด ์๋ค๊ณ ๋๋ผ๋ ๋ฌธ์ ์น์ ์ ์ง์ ํด ์ฃผ์ธ์.) ๊ทธ๋๊น์ง๋ Redux๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ด๋ป๊ฒ ์๋ํ๋์ง์ ๋ํ ์คํด์ ๋ถ๊ณผํฉ๋๋ค.
@markerikson ๊ฐ์ฌํฉ๋๋ค.
๋ฌธ์ ์ ๋ณด:
๋ฐ๋ผ์ ๋ฆฌ๋์๊ฐ ์๋ ค์ง์ง ์์ ์ก์ ์ผ๋ก ํธ์ถ๋๋ฉด ๋ฆฌ๋์๋ ์ด ๋ฆฌ๋์์์ ์ก์ ์ด ๋ฌด์์ ํด์ผ ํ๋์ง ์์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ๋์ผํ ์ํ๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
์คํ ์ด๋ฅผ ์์ฑํ ๋ Redux๋ ์ ์ ์๋ ์์
๊ณผ ์ด์ ์ํ undefined
๋ฅผ ๋ณด๋ด ๋ฆฌ๋์๋ฅผ ํ์ธํฉ๋๋ค. ๋ฐ๋ผ์ ์ด์ ์ํ๊ฐ undefined
์ด๋ฉด ๋ฆฌ๋์๋ ๋ฌธ์์ ๋ฐ๋ผ undefined
๋ฐํํด์ผ ํฉ๋๋ค(๋ฆฌ๋์๋ ์์
์ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์). ๊ทธ๋ฌ๋ ๊ฐ์๊ธฐ๊ฐ undefined
๋ฐํํ๋ฉด Redux ์ฑ์ด ์๋ํ์ง ์์ ๊ฒ์์ ๋ณด์ฅํฉ๋๋ค.
์์ ์ฝ๋์ ๊ฒฝ์ฐ:
import { createStore } from 'redux';
const configReducer = (config: any, action: any): any =>{
return config;
}
const customReducer = (customData: any, action: any): any => {
return customData;
}
const reducers = (currentState: IAppState, action: any): IAppState => {
var appStateToEvaluate: any;
if (currentState) { //needs to add this to pass the `undefined` check of redux
appStateToEvaluate = currentState;
}
else{
//why redux is doing this ?!
appStateToEvaluate = {}
}
const newState: IAppState = {
cvConfig: configReducer(appStateToEvaluate.config, action),
personalData: customReducer(appStateToEvaluate.customData, action)
}
return newState;
}
const defaultConfig = "cool config";
const data = "yieaah some data";
var initialState = {config: defaultConfig, customData: data};
const store = createStore(reducers, initialState) // at this point redux calls all the reducers with 'undefined' state. Why?
@JoseFMP : ์ฌ๊ธฐ์ ๋์น๊ณ ์๋ ์ฃผ์ ์ฐจ์ด์ ์ ํด๋น ์์์ ๋ฆฌ๋์ ํจ์๊ฐ ES6 ๊ธฐ๋ณธ ์ธ์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ state
๊ฐ undefined
์ธ ๊ฒฝ์ฐ๋ฅผ _์ด๋ฏธ_ ์ฒ๋ฆฌํ๋ค๋ ๊ฒ์
๋๋ค.
function todoApp(state = initialState, action) {
๋ฐ๋ผ์ ํํ ๋ฆฌ์ผ์ ์กฐ์ธ์ ์ ํํฉ๋๋ค. ๋ฆฌ๋์๋ _์ ์๋์ง ์์ ๊ฒฝ์ฐ๊ฐ ์ด๋ฏธ ์ฒ๋ฆฌ๋์๋ค๊ณ ๊ฐ์ ํ๊ณ _ ํญ์ ๊ธฐ์กด ์ํ๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
@markerikson
๋ต๋ณ ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.
๊ทธ๊ฒ์ ๋์๊ฒ ๋ถ๋ช
ํฉ๋๋ค. ํํ ๋ฆฌ์ผ์์ ๋๋ฝ๋์์ต๋๋ค. ํํ ๋ฆฌ์ผ์์๋ ๋ฐํํ ์ผ์ด์ค๊ฐ ๋ฆฌ๋์์์ ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์ ๊ฐ์ผ๋ก ์ง์ ๋ ์ผ์ด์ค๋ผ๊ณ ๋งํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ๊ธฐ์ธ์๊ผด๋ก ๋ง๋ ๋ฌธ์ฅ์ด ๋ง์ต๋๋ค. ๊ทธ๊ฒ ๊ทธ๊ฑฐ์ผ. ๋ด ๊ด์ฌ์ฌ๋ ํํ ๋ฆฌ์ผ์์ ๋๋ฝ๋์๋ค๋ ๊ฒ์
๋๋ค. ๋๋ ๋ด๊ฐ ์ฐพ์ง ๋ชปํ๊ฑฐ๋ ๋ชจํธํฉ๋๋ค.
์ด์ ํํ ๋ฆฌ์ผ ๋ฐ/๋๋ ๋ฌธ์์ ๋ณ๊ฐ๋ก ์ด undefined
๊ฒ์ฌ๊ฐ ์ด๊ธฐ ์ํ๊ฐ ์ง์ ๋ ํน์ ๊ฒฝ์ฐ์ ๋ํด ๊ฐ์ธ์ ์ผ๋ก ์๋ฏธ๊ฐ ์๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ์ด๊ธฐ ์ํ๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ๊ด์ฐฎ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ด์ ์ด๊ฒ์ ํ ๋ก ์ด ์๋๋ผ ๋ด ์๊ฒฌ์ผ ๋ฟ์
๋๋ค. ์ด๊ธฐ ์ํ๊ฐ ์ง์ ๋๋ฉด ์ด ๊ฒ์ฌ๋ฅผ ์ํํ๋ ๊ฒ์ด ๋ฌด์๋ฏธํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋๋ ์ด์จ๋ ๊ทธ๊ฒ๊ณผ ํจ๊ป ์ด ์ ์์ต๋๋ค (๊ทธ๋ฆฌ๊ณ ํด์ผํฉ๋๋ค) ;)
๋งํฌ ์ง์ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์ฐ๋ฆฌ๋ ๋ ํฐ ๋ฌธ์ ๊ฐํธ(#2590)์ ์ผํ์ผ๋ก ์ผ๋ถ ๋ฌธ๊ตฌ์ ๋จ์ด๋ฅผ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
์ฆ, Redux์ ์๋ ์์ด๋์ด ์ค ํ๋๋ ๊ฐ "์ฌ๋ผ์ด์ค ๋ฆฌ๋์"๊ฐ ์
๋ฐ์ดํธ์ ์ด๊ธฐ ๊ฐ ์ ๊ณต์ ๋ชจ๋ ํฌํจํ๋ ์ํ ๋ถ๋ถ์ "์์ "ํ ์ฑ
์์ด ์๋ค๋ ๊ฒ์
๋๋ค. ๋น์ ์ "๊ธ์, ๋๋ createStore
์ด๊ธฐ ๊ฐ์ ์ ๊ณตํ๊ณ ์๋ค"๋ผ๋ ์ธก๋ฉด์ ๊ฐํ ๊ฒ ๊ฐ์ง๋ง, ๋น์ ์ _๊ทธ๋ ์ง ์๋๋ผ๋_ Redux๊ฐ ์ด๋ป๊ฒ ํ๋ํด์ผ ํ๋์ง์ ๋ํ ๊ธฐ๋๋ฅผ ํ ์ธํ๊ณ ์์ต๋๋ค. ๊ฐ์ ๋ณ๋๋ก ์ ๊ณตํฉ๋๋ค.
์์ง ๋งํฌํ์ง ์์ ๊ฒ์ด ๋ค์ ๋๋์ง๋ง ๋ฌธ์์ Initializing State ํ์ด์ง๋ฅผ ์ฝ๊ณ ์ถ์ ์๋ ์์ต๋๋ค.
@markerikson
๊ทธ๋ ์ ํ ํด. ๋๋ ์ด๋ฏธ ์ด๊ธฐ ์ํ์ ๋ํ ๋ฌธ์๋ฅผ ์๊ณ ์์ต๋๋ค. ๋ด๊ฐ ์๋ฏธํ๋ ๋ฐ(๊ทธ๋ฆฌ๊ณ ๋ด๊ฐ ๋ฏฟ๋ ์ด์ ๋ ์ด ๋ฌธ์ ๊ฐ ์์ฑ๋์๊ณ ์ฌ๋๋ค๋ ์ด ๋ฐฉํฅ์ ์๋ฏธํ๊ธฐ ๋๋ฌธ์
๋๋ค)์ ์์ ์ ์์ฑํ ๋ "์ด๊ธฐ ์ํ"๋ฅผ ์ ๊ณตํ๊ณ ์ฌ์ ํ "๊ธฐ๋ณธ ์ํ"๋ฅผ ์ ์ํ๋ ๊ฒ์ด ์ง๊ด์ ์ด์ง ์๋ค๋ ๊ฒ์
๋๋ค. ์ฌ๋ผ์ด์ค ๋ฆฌ๋์(๋๋ ๋ฃจํธ ๋ฆฌ๋์). ์ฆ, ๋งค์ฐ ์์ฃผ(๋ฐ๋์ ๊ทธ๋ฐ ๊ฒ์ ์๋์ง๋ง) ๋์ผํ๊ฑฐ๋ ๋งค์ฐ ๊ด๋ จ์ด ์๊ธฐ ๋๋ฌธ์ ๋ ํญ๋ชฉ์ ์ ์ํด์ผ ํ๋ ๊ฒ์ ์ง๊ด์ ์ด์ง ์์ต๋๋ค. @ElonXun ๋๋ @Vittly ๊ฐ ์ ์ ๊ฐ์ ํผ๋์ ์๋ก ๋ค ์ ์์ต๋๋ค. ์ฆ, ๋ด ๋ง์ Redux์ API๊ฐ ์๋๋ผ ์์ ํ ์ธ๊ฐ์ ์ธ ๊ด์ ์์ ์ด ํน์ ์๋๋ฆฌ์ค์์ Redux์ API๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ง๋ ์ง๊ด์ ์ธ์ง์ ๋ํ ๊ฒ์
๋๋ค.
๋ง์ง๋ง ๋จ๋ฝ์ redux์ API๋ฅผ ์ฌ์ฉํ ๋ ์ธ๊ฐ์ ๋๋์ ๊ดํ ๊ฒ์ ๋๋ค. ๊ธฐ๊ณ ๊ตฌํ ๋๋ ๊ทธ ์ด๋ฉด์ ์ด์ ๋ ์์ ํ ์ ๋นํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ API ์๋น์๋ก์ ํผ๋์ค๋ฝ์ต๋๋ค.
์๋ฅผ ๋ค์ด, ์ ๋ ๊ฐ๋ฐํ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ด๊ธฐ ์ํ๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ๊ทธ๋์ ๋ณดํต ๋ ๋ฒ ์ ๋ ฅํด์ผ ํฉ๋๋ค. ์ ์ฅ์๋ฅผ ๋ง๋ค ๋ ํ ๋ฒ ์ฐ๊ฒฐํ๊ณ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์์์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋ฐฐํฌํ ๋ ํ ๋ฒ ๋. ๋ฌผ๋ก ์ด์ ๋ํ ๋ง์ ์๋ฃจ์ ์ด ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ธ๊ฐ์ธ ๋๋ฅผ ํผ๋์ค๋ฝ๊ฒ ๋ง๋๋ ์์น์ ๋์ผํ ๊ฒ์ ๋ ๋ฒ ์ ๋ ฅํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ธฐ ์ํ๊ฐ ์ค์ ๋๊ณ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์ ๋๋ ๋ฃจํธ ๋ฆฌ๋์๊ฐ "๊ธฐ๋ณธ ์ํ"๋ฅผ ๊ฐ๋๋ก ๊ฐ์ ํ์ง ์๋ ํน๋ณํ ๊ฒฝ์ฐ๊ฐ ์ฌ์ ํ ํ์๋ก ๋ง๋๋ ๊ฒ๋ณด๋ค ๋ ๋ฌธ์ ๊ฐ ๋๋ค๊ณ ์๊ฐํฉ๋๋ค.
๊ทธ๋์ ์ฌ๊ธฐ์์ ์ ์ผํ ๊ธฐ์ฌ๋ ๊ทธ๊ฒ์ด ์ฝ๊ฐ ์ง๊ด์ ๋ฐํ๋ ๋๋์ด ๋ ๋ค๊ณ ์ธ๊ธํ๋ ๊ฒ์ ๋๋ค. ํ์ง๋ง ๊ทธ๋ฅ.
์ด์ ๋ฒ์ ์์ assertReducerShape
๋ฅผ ํธ์ถํ combineReducers
๊ธฐ์ต๋์ง ์์ต๋๋ค. ๋ด ์ฝ๋์์ undefined
๋ ๋ด ๊ฐ์๊ธฐ์ ์๋ชป๋ ์ํ์
๋๋ค. ์ด๋ฅผ ๋ณด์ฅํ๊ธฐ ์ํด CombineReducers๊ฐ ํ์ํ์ง ์์ต๋๋ค. "๋ฆฌ๋์๋ฅผ ๊ฒฐํฉ"ํ๊ณ ๋ฌด์ธ๊ฐ๊ฐ ๋ณ๊ฒฝ๋๋ฉด ๋ฃจํธ ๊ฐ์ฒด๋ฅผ ๋ค์ ์์ฑํ๋ ๋ฐ ํ์ํฉ๋๋ค. ๋ด๊ฐ ๊ธฐ๋ํ๋ ๊ฒ ์ด์์ผ๋ก ์งํ๋๊ณ ์์ต๋๋ค. IMO, ์ง๊ธ์ ๋๋ฌด ๋
๋จ์ ์
๋๋ค.
๋ด๊ฐ ๊ฐ๋ฐํ ํต์ฌ ์์ฉ ํ๋ก๊ทธ๋จ์์ ํ์ํ์ง ์์๊ธฐ ๋๋ฌธ์ ๋๋ CombineReducers๋ฅผ ํ๋์ ์ฌ์ฉํ์ง ์์์ต๋๋ค. ํ์ง๋ง ์ง๊ธ์ ๊ทธ ์์ฉ ํ๋ก๊ทธ๋จ์ ๋ํํ๋ ค๊ณ ํ๊ณ ์์ผ๋ฉฐ CombineReducers๊ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์กฐ๊ฐํํ๋ ๋ฐ ์ฌ์ฉํ๊ธฐ์ ์ ํฉํ๋ค๊ณ ์๊ฐํ์ต๋๋ค. assertReducerShape
์ ๋์์ ๋๋์ต๋๋ค.
@lukescott : ๊ทธ ์ํ๋ 2015๋ 9์๋ถํฐ ์์์ต๋๋ค.
https://github.com/reduxjs/redux/commit/a1485f0e30ea0ea5e023a6d0f5947bd56edff7dd
๊ทธ๋ฆฌ๊ณ ์, combineReducers()
์ _์๋์ ์ผ๋ก_ ๋
๋จ์ ์
๋๋ค. ์ด๋ฌํ ๊ฒฝ๊ณ ๋ฅผ ์ํ์ง ์์ผ๋ฉด ํด๋น ๊ฒ์ฌ ์์ด ์ ์ฌํ ๊ธฐ๋ฅ์ ์์ฑํ๋ ๊ฒ์ด ์ฝ์ต๋๋ค.
๊ตฌ์ฒด์ ์ธ ์๋ก ๋ ์ฒดํฌ ์์ด 100์ค๋ก ๋ Dan์ ์์ง
์๊ฒ ์ด. ์ด์ ๋ฒ์ ์ ์ด๊ธฐ ์ํ๋ฅผ ์ ๋ฌํ๊ณ ๊ฐ ์คํ์์ ์ ์๋์ง ์์ ํญ๋ชฉ์ ํ์ธํ์ต๋๋ค(๊ธฐ์ตํ๋ ๊ฒฝ์ฐ). ๋๋ผ์ด ๊ฒ์ ์ฒซ ๋ฒ์งธ ์คํ์์ ์ด๊ธฐ ์ํ๊ฐ ์ ๋ฌ๋์ง ์๋๋ค๋ ๊ฒ์ ๋๋ค. ์ ์๋์ง ์์ ์ ๋ฌ์ ๋ด ์ฑ์ ์ค๋ฅ์ ๋๋ค. ๋ด๊ฐ ๋งํ๋ฏ์ด, ๋๋ ์ด ํ๋ก์ ํธ์ ๋ํด ์ ์ ๋์ ์์ ํ๊ณ ์๊ณ ํ๋์ CombineReducers๋ฅผ ์ฌ์ฉํ์ง ์์์ต๋๋ค. ์ด์ ๋ง ๋ค์ ์ฌ์ฉํ๊ธฐ ์์ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ํผ ์์ ๋ฐฐ์นํฉ๋๋ค.
๊ด๋ ์ ์ด๋ผ๋ ๊ฒ๋ ์ดํดํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ ์๊ฒฌ์ "๋ฆฌ๋์๋ ์ ์๋์ง ์์ ๊ฐ์ ๋ฐํํด์๋ ์๋๋ค"๋ ๊ฒ์ด์์ต๋๋ค. ์ด๊ฒ์ด ์ ๊ฐ ์ค์ํ๋ ๊ท์น์ ๋๋ค. "์ฐ๋ฆฌ๋ undefined๋ฅผ ์ ๋ฌํ ๊ฒ์ด๊ณ ๋น์ ์ ๋ฌด์์ ์ค์ค๋ก ์ํ๋ฅผ ์์ฑํด์ผ ํฉ๋๋ค"๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค. CombineReducers๋ ๋ ์ด์ "ํญ์ ์ด๊ธฐ ์ํ๊ฐ ์์"๊ณผ ํจ๊ป ์๋ํ์ง ์์ต๋๋ค. ์ด๋ ๋ถํํ ์ผ์ ๋๋ค. ์ด๋ 3๋ ์ ์ ๊ท์ ์ ๋ํญ ๋ณ๊ฒฝํฉ๋๋ค.
๋๋ ๋น์ ์ด ๋งํ๋ ํ๋ ๋ณํ๊ฐ ๋ฌด์์ธ์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๊ตฌ์ฒด์ ์ธ ์๋ฅผ ๋ค ์ ์์ต๋๊น?
์ด๊ธฐํ ์ฃผ ๋ฌธ์ ํ์ด์ง ์ฌ์ด์ ์ํธ ์์ฉ์ ๋ฐฐ์นํฉ๋๋ค preloadedState
๋ํ ์ธ์ createStore
์ state = initialState
๊ฐ์๊ธฐ์ ๋ํ ์ฒ๋ฆฌ, ๋ฐ combineReducers()
. ์ด๋ Dan์ด 2016๋
์ด์ ์์ฑํ Stack Overflow ๋ต๋ณ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ ๋ด๊ฐ ์๋ ํ ์๋ฏธ ์๋ ๋ณ๊ฒฝ ์ฌํญ์ ์์ต๋๋ค.
@markerikson ๋ง์ต๋๋ค. ๋๋ 2.0๊น์ง ๋๋์ ๋ณด์๊ณ ํญ์ ๊ทธ๋ฌ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์๋ง๋ ๋ด ์์ฉ ํ๋ก๊ทธ๋จ์ ๋ณต์ก์ฑ์ผ๋ก ์ธํด ๋ ๋ช ํํด์ก์ต๋๋ค.
๋ด๊ฐ ๊ฐ์ง ๋ฌธ์ ๋ ๋ด ๊ฐ์๊ธฐ๊ฐ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋๋ค๋ ๊ฒ์ ๋๋ค.
reducer(state: State, action: Action)
์ํ๊ฐ ์ ์๋์ง ์์์ผ ํจ์ ์๋ฏธํฉ๋๋ค. ์ฆ, ์ด๊ธฐ ์ํ๊ฐ ์์ด์ผ ํฉ๋๋ค. ๊ทธ๋ฌ๋ CombineReducers๋ ํ์ธ์ ์ํด reducer(undefined, INIT)๋ฅผ ํธ์ถํ๊ธฐ ๋๋ฌธ์ ๋ด ์ฝ๋์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค(์ฑ๊ณตํ๋ฉด ๋์ค์ reducer(initState, INIT)๋ฅผ ํธ์ถ - INIT๋ฅผ ๋ ๋ฒ ํธ์ถ).
์ด๊ฒ์ CombineReducers์์ ์ฌ์ฉ๋๋ ๋ชจ๋ ๊ฐ์๊ธฐ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํด์ผ ํจ์ ์๋ฏธํฉ๋๋ค.
reducer(state: State | undefined, action: Action)
๋ฐ๋ผ์ ์ด์ ์๋ ๊ทธ๋ ๊ฒ ํ์ง ์์๋ค๋ ๋ด ์ฃผ์ฅ์ ์๋ชป๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฌ๋ ๋ด๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฌธ์ ์ OP๊ฐ ๊ฐ์ง ๋ฌธ์ ๋ ๋์ผํ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค. ์ ํ์ ์ธ ์ํ๋ก ๊ฐ์๊ธฐ๋ฅผ ์ ์ธํด์ผ ํฉ๋๋ค. ์ค์ ๋ก ๊ฒฝ๊ณ ๊ฐ ํ์๋์ง ์์ต๋๋ค. ์ ์๋์ง ์์ ์ํ๊ฐ ์์๋๊ธฐ ๋๋ฌธ์ ์ฝ๋๊ฐ ์ถฉ๋ํฉ๋๋ค.
์ด๋ ๊ฒ ํด์ผ ํ๊ณ ๋ด๊ฐ ์ง์ ๊ตด๋ ค์ผ ํ๋ค๊ณ ํ๋ฉด ๊ด์ฐฎ์ต๋๋ค. ๋ชจ์์ ์ฃผ์ฅํ๋ ๊ฒ ์ธ์๋ ๋ฐํ์์ ์ด๋ฏธ ์ ์๋์ง ์์ ๊ฒ์ ํ์ธํ๊ธฐ ๋๋ฌธ์ ๋ถํํฉ๋๋ค. ๋ชจ์์ ์ฃผ์ฅํ๋ ๊ฒ์ ๋ค์ ๊ณผ๋ํ๊ณ ๋น์์ฐ์ ์ธ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค.
์. ํด๋น ๋ฌธ์ ํ์ด์ง์ ์ค๋ช
๋ ๋๋ก combineReducers
๋ฅผ ์ฌ์ฉํ ๋ ๊ฐ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์๋ ๊ณ ์ ํ ์ด๊ธฐ ์ํ๋ฅผ ๋ฐํํ ์ฑ
์์ด ์์ผ๋ฉฐ sliceReducer(undefined, action)
์ ๋ํ ์๋ต์ผ๋ก ์ด๋ฅผ ์ํํด์ผ ํฉ๋๋ค. combineReducers
์์ ์์ ์ฝ๋๊ฐ _is_ ๋ฒ๊ทธ๊ฐ ์์ต๋๋ค. ์์๋ ๊ณ์ฝ์ ์ค์ํ์ง ์๊ธฐ ๋๋ฌธ์ ์ฝ๋๊ฐ ์๋ชป๋์๋ค๋ ๋ฉ์์ง๊ฐ ํ์๋๊ธฐ ๋๋ฌธ์
๋๋ค.
์ค์ ๋ก ์ด๊ธฐ ์ํ๋ฅผ ์ ๊ณตํ์ง ์์ต๋๊น? ์ฝ๋๋ ์ค์ ๋ก ์ด๋ป๊ฒ ์๊ฒผ์ต๋๊น?
createStore๋ฅผ ํตํด ์ด๊ธฐ ์ํ๋ฅผ ์ ๊ณตํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ธฐ ์ํ๋ฅผ ์ ๋ฌํ์์๋ ๋ถ๊ตฌํ๊ณ assertReducerShape๊ฐ ๋ช
์์ ์ผ๋ก undefined
๋ด ๊ฐ์๊ธฐ๋ฅผ ํธ์ถํ๊ธฐ ๋๋ฌธ์ ์ด๊ธฐ ์ํ๊ฐ ๋ด ๊ฐ์๊ธฐ๋ก ์ ๋ฌ๋์ง ์์ต๋๋ค.
๋ด ์ฝ๋๋ ๋ฒ๊ทธ๊ฐ ์์ต๋๋ค. undefined๊ฐ _์ ๋_ ์ ๋ฌ๋์ง ์๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ์ด๊ธฐ ์ํ๋ ํ์์ด๋ฉฐ ์๋ฒ์์ ์์ฑ๋ฉ๋๋ค. CombineReducers๋ ํด๋น ๊ณ์ฝ์ ๊นจ๊ณ ํ์ํ ์ํ๋ก ๊ฐ์๊ธฐ๋ฅผ ์๊ฒฉํ๊ฒ ์ ๋ ฅํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๊ฒ ๋ง๋ญ๋๋ค. CombineReducers ์์ด ์ด ์์ ์ ์ํํ ์ ์์ผ๋ฉฐ ์ ์๋ํฉ๋๋ค. ๊ทธ๊ฒ์ด ๋ด๊ฐํด์ผ ํ ์ผ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฌ๋ IMO๋ ์ ํ์ ์ํ๋ฅผ ๊ฐ์ ํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์์ผ๋ฉฐ ์ ๊ฒฝ์ฐ์๋ ์๊ฒฉํ๊ฒ ํ์ํ๋ ์์ฉ ํ๋ก๊ทธ๋จ์ ์ค๋จํ๊ธฐ ๋๋ฌธ์ CombineReducers๋ฅผ ์ธ๋ชจ ์๊ฒ ๋ง๋ญ๋๋ค.
๋ด๊ฐ ์ดํดํ์ง ๋ชปํ๋ ๊ฒ์ assertReducerShape๊ฐ ํ์ํ ์ด์ ์
๋๋ค. ๋ฐํ์ ์ ์ฌ๊ธฐ์์ ์ด๋ฏธ undefined
๋ฅผ ํ์ธํฉ๋๋ค.
assertReducerShape๋ ์ผ์ข ์ ์ค๋ณต๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋ฌ๋ ์ ๊ฒฝ์ฐ์๋ ์ ํ ๋ณด์ฅ์ด ์ค๋จ๋ฉ๋๋ค... ์ด๊ฒ์ด ์ฃผ์ฅํ๋ ค๋ ๊ฒ์ ๋ฌด์์ ๋๊น?
๊ทธ๋ ๋ค๋ฉด ์ด๊ฒ์ ์ค์ ๋ก TypeScript ์ํธ ์์ฉ ๋ฌธ์ ์ ๋๊น?
์์งํ ๋งํ์๋ฉด ์ด๋ ๊ฒฐ๊ตญ ์ ๊ทผ ๋ฐฉ์์ ์ฐจ์ด์ธ ๊ฒ ๊ฐ์ต๋๋ค.
combineReducers()
์ JS๋ก ์์ฑ๋์์ผ๋ฉฐ ๋ฐํ์ ๊ฒ์ฌ๋ฅผ ํ๋ ค๊ณ ํฉ๋๋ค.
TS์์ ์ ์ ๊ฒ์ฌ๋ฅผ ํ๋ ค๊ณ ํ๋๋ฐ ์์ฑํ ์ ์ธ์ด combineReducers()
์ค์ ์๋ ๋ฐฉ์๊ณผ ์ผ์นํ์ง ์์ต๋๋ค. ๊ทธ๋ฐ ์๋ฏธ์์ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์์ ์ ํ ์ ์ธ์ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. ์๋ํ๋ฉด combineReducers()
์ ํจ๊ป ์ฌ์ฉ๋ ๋ undefined
๊ฐ _can_ ๋ฐ _will_ ํธ์ถ๋๊ธฐ ๋๋ฌธ์
๋๋ค.
ํธ์ถํ ํน์ ๋ผ์ธ์ ์ฌ๋ผ์ด์ค ๋ฆฌ๋์์ ๋ฐํ ๊ฐ์ด (์๋ง๋ ์๋ฏธ ์๋) ๊ธฐ์กด ์ํ ์ฌ๋ผ์ด์ค ๊ฐ์ผ๋ก ํธ์ถ๋ ๋ undefined
๊ฐ ์๋์ง ํ์ธํ๋ ๋ฐ๋ฉด assertReducerShape()
๋ ๋ค์์ ํ์ธํฉ๋๋ค. ๋ฐํํ์ง ์์ต๋๋ค undefined
์ ์ด๊ธฐ ์ํ ๊ฐ์ด ์ฃผ์ด ์ก์ ๋ undefined
, ์ฆ ์ ์์๋ ์์
์ ํ (ํธ์ถ ์์์ ๊ฒฝ์ฐ์๋ (์ฆ, ์ฆ, ๊ฐ์๊ธฐ์ ์ํ๋ฅผ ์๊ธฐ - ์ด๊ธฐํ) ๋ฐ ๊ฐ์๊ธฐ ๊ธฐ๋ณธ์ ์ผ๋ก ํญ์ ๊ธฐ์กด ์ํ๋ฅผ ๋ฐํํฉ๋๋ค.
์ ์๋์ง ์์ ๊ฒฝ์ฐ ๊ต์ฒดํ์ญ์์ค ... ์ ๊ฒฝ์ฐ์๋ ์ด๋ค ์ด์ ๋ก ๋ ๋ด ์๋ฒ๊ฐ ๋ฐ์ดํฐ์์ด ์๋ต ํ ๋ ํ์ฑ ์ธ์ ์ด ์๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ๊ฐ์๊ธฐ์์์ด ์์ ์ ์ํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์๋ํฉ๋๋ค. ๋๋ฅผ ์ํ ๋งค๋ ฅ:
export default function itemReducer(state = initialState.items | undefined, action){
switch(action.type)
{
case "LOAD_ITEMS_SUCCESS":
if(action.items==undefined) {
action.items=[];
}
return action.items
๋ฆฌ๋์๊ฐ ์ ์๋์ง ์์ ๊ฒฝ์ฐ ๋น ๋ฐฐ์ด์ undefined๋ก ๋ฐ๊พธ๋ฉด ๋ ์ด์ ํด๋น ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
๊ฑด๋ฐฐ!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฒ์ ์ง์ ์์คํ ์ด ์๋ ๋ฒ๊ทธ ์ถ์ ๊ธฐ์ ๋๋ค. ์ฌ์ฉ ์ง๋ฌธ์ ์คํ ์ค๋ฒํ๋ก ๋๋ Reactiflux๋ฅผ ์ฌ์ฉํ์ญ์์ค. ๊ฐ์ฌ ํด์!