2017-09-15 336 views
1

我想在mapStateToProps內使用三元語句,但它似乎並沒有更新狀態,我仍然跑mapStateToProps must return a plain object. Instead received undefined.此語法不正確?mapStateToProps必須返回一個普通對象。相反收到undefined

const mapStateToProps = state => { 
console.log("current state is:" + JSON.stringify(state.model)); 
state.model.selectedPerson ? 
    { 
     selectedCourse: state.model.selectedPerson.selectedCourse, 
     startDate: state.model.selectedPerson.startDate, 
     courseType: state.model.selectedPerson.courseType, 
    } : 
    { 
     selectedCourse: [], 
     startDate: '', 
     courseType: '', 
    }; 
}; 

const reducers = Object.assign({}, { model: courseReducers }); 
export { reducers }; 

export default connect(mapStateToProps, mapDispatchToProps) 
(AddCourseDialog); 

我想在自定義模式中使用一個窗體,上面的容器包含在其他2個父容器中。我還沒有探索使用redux形式呢。

回答

1

方法mapStateToProps你應該返回一個對象。更改爲此

const mapStateToProps = state => { 
console.log("current state is:" + JSON.stringify(state.model)); 
return state.model.selectedPerson ? 
    { 
     selectedCourse: state.model.selectedPerson.selectedCourse, 
     startDate: state.model.selectedPerson.startDate, 
     courseType: state.model.selectedPerson.courseType, 
    } : 
    { 
     selectedCourse: [], 
     startDate: '', 
     courseType: '', 
    }; 
}; 
1

正如其他答案所說,您只需要返回對象。這裏有可能提取值,並使用object destructuring提供默認清潔方法:

const mapStateToProps = (state) => { 
    console.log("current state is:" + JSON.stringify(state.model)); 
    const { 
    selectedPerson: { 
     selectedCourse = [], 
     startDate = '', 
     courseType = '' 
    } = {} 
    } = state.model; 

    return { 
    selectedCourse, 
    startDate, 
    courseType 
    }; 
}; 
+0

我沒有看到任何以上,但狀態不會得到更新控制檯上的錯誤。我不認爲減速器在調度過程中被調用。有沒有辦法在reducer中放入console.log語句? – linuxNoob

相關問題