2017-05-22 3 views
0

저는 Redux를 처음 사용하고 기본적인 작업을 수행하는 동안 문서를 읽는 중입니다.redux reducer가 상태를 업데이트하지 않습니다.

목록에 항목을 추가 할 수있는 감속기가없는 것 같습니다. 올바른 액션 작성자가 발사하고 있습니다. 제 생각에는 이해가 안되는 제 Object.assign 문장에 뭔가가있을 것 같습니다. 아래는 내 store.js 파일입니다.

const defaultState = { 
    todos:[ 
    { 
    text: 'walk gilbert' 
    }, 
    { 
    text: 'cook dinner' 
    }, 
    { 
    text: 'clean bathroom' 
    } 
] 
} 

function todos(state = defaultState) { 
    return state; 
} 

function modifyList(state = defaultState, action) { 
    switch(action.type) { 
    case 'ADD_TODO': 
    return Object.assign({}, state, { 
     todos: [ 
      ...state.todos, 
     { 
      text: action.text, 
     } 
     ] 
    }) 

    default: 
    return state; 
} 
} 

const rootReducer = combineReducers({todos, modifyList}) 

const store = createStore(rootReducer, defaultState); 

export default store; 

감사!

+0

전체 코드를 제공 할 수 있습니까? 행동을 어디에서 파견하고 있습니까? 문제를 재현하려했지만 시도 할 수 없습니다. https://codepen.io/olivercs/pen/dWwzpj?editors=0012 –

+0

내 repo는 https://github.com/AntonEmery/react-redux-todo/tree/todo-list-redux/react-app –

답변

2

마치 combineReducers이 어떻게 작동하는지 조금 혼란스러워합니다.

combineReducers 유틸리티는 상태 트리 개체의 필드 또는 "슬라이스"를 정의하고 이러한 슬라이스를 업데이트하는 작업을 특정 기능에 위임하기위한 것입니다. 귀하의 경우 실제로는 state.todos 슬라이스가 필요하지만, combineReducers()은 실제로 state.todosstate.modifyList을 생성합니다. 또한 combineReducers을 사용하면 각 슬라이스 감속기는 전체 상태 트리 하나만 볼 수 있습니다. 즉, todos() 감속기 내부에서 state 매개 변수는 todos 섹션입니다.

const defaultTodosState = [ 
    {text : 'walk gilbert'}, 
    {text : "cook dinner"}, 
    {text : "clean bathroom"} 
]; 

function todos(state = defaultTodosState, action) { 
    switch(action.type) { 
    case 'ADD_TODO': { 
     return [ 
      ...state, 
      {text: action.text} 
     ] 
    } 
    default: 
     return state; 
    } 
} 

const rootReducer = combineReducers({todos}); 

당신은 일반적으로 combineReducers 및 감속기를 논의 돌아 오는 워드 프로세서의 섹션을 읽어보십시오 :

그럼, 당신이 원하는 것은 더이 같은 것입니다 Introduction - Core Concepts, Basics - Reducers, API Reference - combineReducers, 그리고 Structuring Reducers - Using combineReducers .

+0

에 있습니다. 이 링크를 한번보세요 : - https://stackoverflow.com/questions/49152672/render-function-not-getting-call-after-dispatching-action-from-component – Siddharth

관련 문제