i want to make a request to my php page to retrieve data until the page is loaded. i made this request with axios’ this request is successful “. i want to change state in store “the state tank is successful but the change is no”. I found an error: TypeError: Cannot read property ‘props’ of undefined, at ‘componentDidMount handle succes’ level.
JavaScript
x
32
32
1
the code of the error page:
2
import React from "react";
3
import Axios from 'axios';
4
import {connect} from 'react-redux';
5
const mapDispatchtoprops=(dispatch)=>{
6
return{
7
load_state:(store)=>dispatch({type:"load_produit_to_state",payload:store})
8
}
9
}
10
const mapStateToProps =(state)=>{
11
return{
12
state,
13
}
14
};
15
class Produit extends React.Component{
16
componentDidMount(){
17
let request = new FormData;
18
request.append("getters_produit","true");
19
Axios.post('http://localhost/e_commerce/src/php/get.php',request).then(function(reponce){
20
//handle succes
21
this.props.load_state(reponce.data.liste_produit);
22
}).catch(function(error){
23
alert(error);
24
}).then(function(){
25
//always executed
26
});
27
}
28
render(){
29
return(<div className="les_produit">
30
{ console.log( this.props.state), console.log( "from props")}
31
</div>)}
32
}
JavaScript
1
13
13
1
store page:
2
import {createStore} from 'redux';
3
const reducer =(state,action)=>{
4
switch(action.type){
5
case 'load_produit_to_state':alert(action.payload); return{ liste_produit: action.payload };
6
default: return state;
7
}
8
}
9
const initialstate = {
10
liste_produit:"none",
11
profile:"nono"
12
}
13
export default createStore(reducer,initialstate)
Advertisement
Answer
JavaScript
1
14
14
1
componentDidMount(){
2
const {load_state} = this.props // You Need This
3
let request = new FormData;
4
request.append("getters_produit","true");
5
Axios.post('http://localhost/e_commerce/src/php/get.php',request).then(function(reponce){
6
//handle succes
7
load_state(reponce.data.liste_produit);
8
}).catch(function(error){
9
alert(error);
10
}).then(function(){
11
//always executed
12
});
13
}
14