Skip to content
Advertisement

How updated object with child component should reflect on parent component

I want to add the macchines in machine array so I defined a specific component with add function in it. So when I add the “process” in “processes” array then it is reflecting on the console using useEffect. But when I add a machine it is reflected in MachineGround Component But not in App component. Overall I am planning to add a dashboard where if even a mcahine is added in machines array it should reflect in the processes in App Component and the dashboard should be updated.

I will appreciate your help.

App component

JavaScript
JavaScript

I am thinking that should I use Redux ? or another state management then what should I do? I messed up the states.

Advertisement

Answer

State management tools like Redux, Context-API can be a good option here but even if you do not want to use them, you can make use of normal JavaScript functions. Just pass them as props from your parent component to child component.

I will explain what I mean here. Write a function in your parent component which take a machine object and update the machines array. Now pass this component as props to your child component. Now inside your child component call this function with the machine object that you want to add to your machines array. And boom, your machines array in parent will be updated.

Advertisement