无法读取未定义的属性“类型”(操作)

时间:2020-08-27 21:08:13

标签: javascript reactjs redux react-redux

我正在使用React + Redux。我遇到错误:

Error

在调试时,来自服务器的数据进入状态,并且操作为空:

action empty

这是我的代码的一部分:

class ProfileContainer extends React.Component {
    componentDidMount() {
        debugger
        axios.get('https://social-network.samuraijs.com/api/1.0/profile/3')
            .then(response => {
                this.props.setUserProfile(response.data)
            })
    }
}
export default connect(mapStateToProps, { setUserProfile })(ProfileContainer);

我的减速器:

const initialState = {
    posts: [
        { id: 1, message: "Hey, how are you?", likesCount: 3 },
        { id: 2, message: "This is my first post", likesCount: 15 },
        { id: 3, message: "This is my first post", likesCount: 17 },
    ],
    newPostText: "",
    profile: null
};

const profileReducer = (state = initialState, action) => {
    debugger
    switch (action.type) {
        case SET_USER_PROFILE:
            return { ...state, profile: action.profile };

        default:
            return state;
    }
};
export const setUserProfile = (profile) => ({ type: SET_USER_PROFILE, profile })

2 个答案:

答案 0 :(得分:0)

启动调度,将结果传递给dispatch()函数:

export const setUserProfile = (profile) => (dispatch({ type: SET_USER_PROFILE, profile }))

答案 1 :(得分:0)

您的调度流程是错误的,您必须这样做:

const mapDispatchToProps = dispatch => {
  return {
    // dispatching plain actions
    setUserProfile: (profile) => dispatch({ type: SET_USER_PROFILE, profile }),
  }
}

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