So basically, I have a delete button in my parent component and whenever I click on that button, some function should be called that will change or reset a state variable of child component. I am unable to think of a solution here. I basically am a Java developer but now have to handle some frontend stuff as well that involves React. Any help would be appreciated!
Unable to think of an approach here.
3
Answers
You should learn more about the basics of React.
Anyway here is an example:
If your state management is going to be more complex and you’ll need to access the values from a diffrent component, or even a diffrent component tree you should use React context or Redux.
Here’s a Codesandbox example
You can lift the state from the child to the parent component and pass the required parent state as a prop to the child component.
You can read more about this here :
https://legacy.reactjs.org/docs/lifting-state-up.html
Like another answer mentioned, you need to "lift" the state up in the parent component, and have that update it. The state can be passed to the child component as a prop.
Here is an example using class components: