Skip to content
Advertisement

React – useState property not updating

I’m having trouble with react useState hook. I’m trying to clean the state on a dialog close. But theres’s always one property that does not gets updated.

Buttons:

JavaScript

—- Edit —-

JavaScript

That button opens a dialog box which has a textfield:

JavaScript

This is how the dialog box open and closes:

JavaScript

This is the state:

JavaScript

This is the edit button click:

JavaScript

The expected functionality is that when I click the edit btn, the modal opens with the post details and when the modal is closed, clean input so whenever I click on new post button the form is clean. However the form only gets clean when I open the modal with the new post button and then close it, not when I open it with the edit button. Everything gets cleaned but the p_title property.

— Edit —

So the problem was in my input onChange function, this is it:

JavaScript

However I don’t know how to solve it. My guess is that when the editor (CKeditor) input changes, it sets p_title to it’s original value.

Advertisement

Answer

I managed to solve it. What was happening is that whenever I updated the input state, the ckeditor component detected change so it changed the input state to the one that I set before closing the dialog box.

The solution was to create a diferent function to update ckeditor state, so the whole state doesn’t change when ckeditor updates.

This was the new function to update ckeditor state

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