Reactjs 在页面加载时以redux形式传递初始值
我将初始值传递给redux表单。我正在获取数据,而组件将挂载 加载页面时,出现错误,无法读取未定义的属性“id”。如果我刷新页面,数据就会在那里 谁能帮帮我吗 这是我的密码Reactjs 在页面加载时以redux形式传递初始值,reactjs,redux,redux-form,Reactjs,Redux,Redux Form,我将初始值传递给redux表单。我正在获取数据,而组件将挂载 加载页面时,出现错误,无法读取未定义的属性“id”。如果我刷新页面,数据就会在那里 谁能帮帮我吗 这是我的密码 componentWillMount() { this.props.actions.getService(this.props.params.Id); } const mapStateToProps = (state) => { return { initialValues: {
componentWillMount() {
this.props.actions.getService(this.props.params.Id);
}
const mapStateToProps = (state) => {
return {
initialValues: {
user_id:state.userInfo.data.user.id,
}
}
}
谢谢。另一种方法是使用Redux表单为您添加的this.props.initialize。您还可以使用lodash的.get方法来避免添加这样的大量检查:if(nextState.userInfo&&nextState.userInfo.data&&nextState.userInfo.data.user&&nextState.userInfo.data.user.id&&nextState.userInfo.data.user.id!==this.state.userInfo.data.user.id) 像这样的方法应该会奏效:
componentWillReceiveProps = (nextProps) => {
if (_.get(nextProps, 'userInfo.data.user.id') !== _.get(this.props, 'userInfo.data.user.id')) {
this.props.initialize({ user_id: nextProps.userInfo.data.user.id });
}
}
mapStateToProps = (state) => {
return {
userInfo: state.userInfo,
};
}