Can't add uuid to state onSubmit

Hi guys,

I am working on event manager app and trying to assign unique id via uuid to each event. Somehow the individual state object is not storing the eventId but every other detail:

Here is the link to my project: https://codesandbox.io/s/sharp-sun-9p1w5?file=/src/App.js All the code is in App.js file and the relevant code where I am saving the id to the state object is between line 102-106

Any help would be much appreciated.

Hey there,

I didn’t test my idea, but I think because setState is async, you run this.props.... with the old state.

I think you have to wait for the setState,
setState can take a callback function,
you can read it in the React Docs.

1 Like

Hi,

Sorry for the late reply and thank you for your reply. I tried the async way and it did not help.

Weirdly, if I set the event Id in any other method like handleEventName(onClick), it works. But when I try to set the event id in handleSubmitForm(onSubmit), the setState doesn’t seem to work.

Can you elaborate on this?

so I used something like this:

this.setState((prevState) => {...this.prevState.CurrentEvent, eventId:eventId} )

Used this syntax so the setState calls can be queued.

Alright,

I meant that you could add your callback after it, like:

this.setState(
  stateChange,
  () => this.props.doSomething(someArguments)
)
1 Like