为什么当状态发生变化时mapStateToProps返回空对象?

时间:2019-02-10 16:02:13

标签: javascript reactjs redux react-admin

我创建动作

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));

商店已突变 enter image description here

但是console.log(this.props.kidsTabData)返回空对象{}。

enter image description here

你能告诉我我错了吗?在我看来,一切都正确地存储了,但是initialState却变成了道具。

1 个答案:

答案 0 :(得分:1)

redux操作kidsLoad是异步的。 在componentDidUpdate

中尝试console.log
componentDidUpdate(){
    console.log(this.props.kidsTabData)
}