我正在尝试在mapStateToProps
中使用三元语句但它似乎没有更新状态而且我一直遇到mapStateToProps must return a plain object. Instead received 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表单。
答案 0 :(得分:2)
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 :(得分: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
};
};
答案 2 :(得分:0)
显然我遇到了同样的错误。我最初的连接方法是
const mapStateToProps = (state, props) => {
return {
post: state.readMore
};
};
const mapActionsToProps = (dispatch, props) => {
// return bindActionCreators(
// {
// onTabChange: tabChange,
// onReadMore: readMore
// },
// dispatch
// );
};
export default connect(
mapStateToProps,
mapActionsToProps
)(ParticularNewsTemplate);
我的我的bindActionCreators()方法被注释掉了(之所以被注释,是因为我没有从 ParticularNewsTemplate 组件传递任何值)。但是取消注释bindActionCreators()方法对我来说是错误。我希望这对某人有帮助。
我的最终代码如下
const mapStateToProps = (state, props) => {
return {
post: state.readMore
};
};
const mapActionsToProps = (dispatch, props) => {
return bindActionCreators(
{
// onTabChange: tabChange,
// onReadMore: readMore
},
dispatch
);
};
export default connect(
mapStateToProps,
mapActionsToProps
)(ParticularNewsTemplate);