React / Redux:为什么在ComponentDidMount()中没有调用我的动作?

时间:2018-12-07 04:32:31

标签: reactjs redux react-redux components action

我正在尝试获取一种在页面加载时加载的方法,我发现做到这一点的最佳方法是使用ComponentDidMount()。我似乎无法激发我的方法。我知道CompoentDidMount()正在工作,因为我能够登录。

这是我的ComponentDidMount():

componentDidMount() {
   this.props.getStus()
}

这是我的mapToDispatch和Connect:

const mapDispatchToProps = dispatch => {
  return{
    getStus: () => dispatch(auth.getStus),
  }
}

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

这是我的操作代码:

export const getStus = () => {
    return (dispatch, getState) => {
        console.log("In getStus");
        let headers = {"Content-Type": "application/json"};
        return fetch(`${url}/api/stu/list/`, {headers, body: "", method: "GET"})
            .then(res => {
                if (res.status < 500) {
                    return res.json().then(data => {
                        return {status: res.status, data};
                    })
                } else {
                    throw res;
                }
            })
            .then(res => {
                if (res.status === 200) {
                    dispatch({type: 'GET_STUS_SUCCESSFUL'});
                    return res.data;
                } else if (res.status === 403 || res.status === 401) {
                    dispatch({type: "AUTHENTICATION_ERROR", data: res.data});
                    throw res.data;
                }
            })
    }
}

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

更改此内容:

const mapDispatchToProps = dispatch => {
 return{
    getStus: () => dispatch(auth.getStus),
   }
}

const mapDispatchToProps = dispatch => {
  return{
    getStus: () => dispatch(auth.getStus()),
   }
}