Hi, I am trying to capture a variable in a child component and pass it to the parent component App.js to update the total amount status. This is the error message:
"Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops."
Next I put the components that interact in the process:
App.js
class App extends Component {
constructor() {
super();
this.state = {
categorias: {},
productos: {},
productosOrden: {},
orden: {},
total: 0,
montoTotal: 0};
}
totalMonto = (valor) =>{
console.log(valor);
const montoTotal = valor;
this.setState({montoTotal});
}
render() {
return (
<div className="App">
<div className="row">
<PedidoTotal
{...this.state}
titulo="Pedido Total"
totalMonto={this.totalMonto}
/>
<CalcularVuelto titulo="Calcule el Cambio"
totalMonto={this.totalMonto}
/>
</div>
</div>
);}
----- Total Order component
class PedidoTotal extends Component{
renderOrden = (key) =>{
const productos = this.props.productosOrden;
console.log(productos);
const prod = this.props.productosOrden[key];
const count = this.props.orden[key];
//this.props.actualizarMonto();
return <li key={key}>
{count}: unidades de {prod.nombre}
:${count * prod.precio}
<button onClick={()=>this.props.deleteOrden(key)}>eliminar</button>
<button onClick={()=>this.props.reduceOrden(key)}>Reducir</button>
</li>
}
render(){
const ordenIds = Object.keys(this.props.orden);
//Calculando el total a pagar
const total = ordenIds.reduce((prevTotal, key)=>{
const prod = this.props.productosOrden[key];
const count = this.props.orden[key];
return prevTotal + (count * prod.precio)
}, 0);
this.props.totalMonto(total);
return(
<div className="col-md-3">
<h3>{this.props.titulo}</h3>
<ul>
{ordenIds.map(this.renderOrden)}
</ul>
<h5>Total a pagar: ${total} </h5>
</div>
);
}
}