我创建动作
export const KIDS_LOAD = 'KIDS_LOAD';
export const kidsLoad = (data) => ({
type: KIDS_LOAD,
payload: data ,
});
我创建减速器
import { KIDS_LOAD } from '../customActions/KidsActions';
export default (state = {}, { type, payload }) => {
if (type === KIDS_LOAD) {
return { ...state,
img: payload.img,
kidsInfo: payload.kidsInfo
};
}
return state;
}
我派遣行动
componentWillMount() {
this.props.setSidebarVisibility(true);
const { kidsLoad, record } = this.props;
kidsLoad({
img : 'https://s18670.pcdn.co/wp-content/uploads/care_about_grades_middle_school.jpg',
kidsInfo: {
kidName : 'Sasha',
kidAge : '19',
kidHandType : 'Right',
kidGender : 'Boy',
}
})
console.log( this.props.kidsTabData)
}
我映射状态
const mapStateToProps = state => ({
kidsTabData: state.kidsReducer,
isLoading: state.admin.loading > 0,
});
export default connect(mapStateToProps, { setSidebarVisibility, kidsLoad })(withStyles(styles)(MyLayout));
但是console.log(this.props.kidsTabData)返回空对象{}。
你能告诉我我错了吗?在我看来,一切都正确地存储了,但是initialState却变成了道具。
答案 0 :(得分:1)
redux操作kidsLoad
是异步的。
在componentDidUpdate
componentDidUpdate(){
console.log(this.props.kidsTabData)
}