I have a component that fails in my componentDidMount when dispatching a Redux action that I have.
Component.
constructor(props){
super(props);
this.state = {
orders: {
status: 1
}
}
}
componentDidMount(){
const { dispatch } = this.props;
let { orders } = this.state;
dispatch(orders.showOrders(orders));
}
action
export const orders = {
showOrders
}
function showOrders(params){
console.log(params);
}
The problem is that it gives me the following error:
TypeError: orders.showOrders is not a function
However, I noticed that when I comment out the line let { orders }
and add an object where the command to call the showOrders function works, the code executes correctly.
Curiously, I tried to assign an object to a variable and add it where I command the showOrders function to be called and I get the same error. For what is this?
You answered yourself hehe, the problem is that in the function you
componentDidMount
declared a variableorders
, and in that scopeorders
it is not the object with your action, but rather the component's state portion!Rename your actions object (to ordersActions for example) or your state object to fix the problem.