I have the following code where I am trying to update the value for the select tag.
The SelectTag is a child component to NameForm which in turn is rendered by the function App(). The change handler resides in SelectTag while the submit handler is in NameForm. I am trying to get the selected value from SelectTag to the parent NameForm by using a callback handleSelectCallback(). When the data in SelectTag is changed it is not being updated in NameForm.
If I start with the value Male and change it to Female, the value of selectValue in NameTag is still Male. If I change the value again (say to Not Specified), the value of selectValue changes to Female.
(Note: I noticed that this is working properly for other React components. I tested with components that render text boxes and text areas.)
Answer:
You are sending the old state value through the callback. setState is async.
You could just change it to the right value
Or better yet, remove the state.value because it’s not need. Instead keep the state in one place (the parent), and send the value and the callback down.
If you have better answer, please add a comment about this, thank you!