Skip to content
Advertisement

Clear React child input value from parent using functional components

I would like to clear child functional component from parent after setting redux state

Here’s my code

JavaScript

On child component

JavaScript

Actually when I press enter text stays , I would like to delete it.

I tried keyPressed.target.value = '' in parent component but it’s not working

Advertisement

Answer

You need to introduce state in the Parent component. Then pass the state value to the input, and provide an onChange handler when the input changes. This is called a controlled component and you can read more about it here.

https://reactjs.org/docs/forms.html#controlled-components

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement