reactn icon indicating copy to clipboard operation
reactn copied to clipboard

setGlobal in componentDidMount should be synchronous.

Open quisido opened this issue 5 years ago • 5 comments

Also did update and set global callback

Async elsewhere

quisido avatar Nov 10 '18 14:11 quisido

Why not declaring componentDidMount async and do await setGlobal?

umbertoghio avatar Jan 28 '19 15:01 umbertoghio

setState is synchronous in componentDidMount. setGlobal should mimic that behavior. It's just a matter of it being possible in all implementations.

You can likely find details from the React team on why setState is synchronous sometimes. I'd love to link it, but I'm restricted to mobile for a little longer as I'm in the process of moving.

quisido avatar Jan 28 '19 20:01 quisido

@CharlesStover Are you referring to this comment?

bpas247 avatar Jun 03 '19 20:06 bpas247

Hi @CharlesStover ,

I feel like having a third parameter to force synchronicity could be interesting.

As of now, what would be the best way of making synchronous calls to setGlobal, regardless of the context ?

Thank you in advance,

matteocargnelutti avatar Sep 04 '19 23:09 matteocargnelutti

I'm not sure this is related, but I've noticed that when I setGlobal in componentDidMount (after async fetching of data), it sometimes does not cause the page to render. I have to use a callback and force update:

setGlobal({ myValue: val }, ()=>{
  this.forceUpdate()
})

EDIT I figured out my problem. I forgot that reactn requires a default value for global state to trigger a re-render the first time it is changed: https://github.com/CharlesStover/reactn/issues/57

stahlmanDesign avatar Sep 16 '19 22:09 stahlmanDesign