reactjs - React/Redux:为什么没有在 ComponentDidMount() 中调用我的操作?
问题描述
我正在尝试获取一种在页面加载时加载的方法,我发现最好的方法是使用 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;
}
})
}
}
任何帮助,将不胜感激。谢谢。
解决方案
改变这个:
const mapDispatchToProps = dispatch => {
return{
getStus: () => dispatch(auth.getStus),
}
}
至
const mapDispatchToProps = dispatch => {
return{
getStus: () => dispatch(auth.getStus()),
}
}
推荐阅读
- windows - 什么是信托提供者?
- python - 迭代数据框列
- node.js - 合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件。构建期间的下一个 js 配置错误
- java - HttpURLConnection Locally 适用于所有 url,但它不在服务器上
- python - 如何检查它是否是python中的新一天?
- r - 如何重命名R列内的项目?
- json - 自动为所有类型派生配置的编解码器
- typo3 - Plesk - TYPO3 10LTS - 客户端在尝试访问页面模块时被服务器配置错误拒绝并被锁定
- python - 使用 matplotlib 查找交点
- python - 使用 Python 在 Yahoo Finance 中抓取 Analysis 选项卡