javascript - 在 redux 中用新状态更新状态
问题描述
我想向我的 php 页面发出请求以检索数据,直到页面加载。我用 axios 提出了这个请求“这个请求是成功的”。我想更改存储中的状态“状态槽成功但更改为否”。我发现一个错误:TypeError:无法读取未定义的属性“props”,位于“componentDidMount 处理成功”级别。
the code of the error page:
import React from "react";
import Axios from 'axios';
import {connect} from 'react-redux';
const mapDispatchtoprops=(dispatch)=>{
return{
load_state:(store)=>dispatch({type:"load_produit_to_state",payload:store})
}
}
const mapStateToProps =(state)=>{
return{
state,
}
};
class Produit extends React.Component{
componentDidMount(){
let request = new FormData;
request.append("getters_produit","true");
Axios.post('http://localhost/e_commerce/src/php/get.php',request).then(function(reponce){
//handle succes
this.props.load_state(reponce.data.liste_produit);
}).catch(function(error){
alert(error);
}).then(function(){
//always executed
});
}
render(){
return(<div className="les_produit">
{ console.log( this.props.state), console.log( "from props")}
</div>)}
}
store page:
import {createStore} from 'redux';
const reducer =(state,action)=>{
switch(action.type){
case 'load_produit_to_state':alert(action.payload); return{ liste_produit: action.payload };
default: return state;
}
}
const initialstate = {
liste_produit:"none",
profile:"nono"
}
export default createStore(reducer,initialstate)
解决方案
componentDidMount(){
const {load_state} = this.props // You Need This
let request = new FormData;
request.append("getters_produit","true");
Axios.post('http://localhost/e_commerce/src/php/get.php',request).then(function(reponce){
//handle succes
load_state(reponce.data.liste_produit);
}).catch(function(error){
alert(error);
}).then(function(){
//always executed
});
}
见这个。
React - TypeError:无法读取未定义的属性“道具”
推荐阅读
- magento2 - 详细 Magento2 事件参考
- python-3.x - 如何在python中从具有非重叠补丁大小32 X 32的图像中提取24维颜色直方图特征向量
- javascript - Nestjs 使用 axios
- angular - 通过孩子向孙子注入祖先的前向参考
- google-app-engine - 在 AppEngine 开发服务器上导入 matplotlib 失败
- azure-devops - 构建在管道中更改的功能分支
- html - 如何填充边距的整个背景颜色?
- https - 服务器和客户端在客户机器上运行,我应该使用 HTTPS,如何使用?
- amazon-web-services - 忽略自动生成的 AWS Amplify 文件
- swift - 为 ARKit(不是 QLPreview)材质属性加载 USDZ