Skip to content
Advertisement

How to clear react state in modal after closing?

I’ve got a product card with product details shown. On the bottom, there is an ‘edit’ button. When clicked it shows a modal with prefilled input fields, that can be edited then saved. Modal can also be closed without saving (but with input fields edited).

My problem is that when a user edits the fields, then closes modal (without saving), then opens it again, fields are not set to initial value but are shown changed.

I’ve tried a variable with the initial state then after closing filling the state with it, but it did not work. Tried to react refs too, no joy.

JavaScript

Advertisement

Answer

If the data from the inputs is in your component you can try something like this : In closeModal you can set the initial state of the component

JavaScript

But if the state of the inputs is coming from the Parent you need a new method to reset the data of the parent component that could be added as a callback in the same method.

JavaScript
Advertisement