Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition is back on setState #19

Closed
vans163 opened this issue Jun 9, 2019 · 3 comments · Fixed by #20
Closed

Race condition is back on setState #19

vans163 opened this issue Jun 9, 2019 · 3 comments · Fixed by #20

Comments

@vans163
Copy link

vans163 commented Jun 9, 2019

So I started using a 3rd party api that loads side by side the initial render. When setGlobalState is called, the first render does not notice the state was changed.

A second forced render a second later, picks up on the change. Something is making observableBits crap out, and I am not sure I could easily replicate this.

Is there anywhere in the code here, if setState gets called, it wont render upon initial render.

EDIT: I traced this bug/feature, hoping its a bug. If the initialState does not define the field, during first render it gets skipped over.

Broken code

initialState = {
}

const [s_user] = useGlobalState("user");

function asynccallback() {
    setGlobalState("user", ()=> {return {email: "bob@test.com"}})
}

Working code

initialState = {
    user: {}
}

const [s_user] = useGlobalState("user");

function asynccallback() {
    setGlobalState("user", ()=> {return {email: "bob@test.com"}})
}
@dai-shi
Copy link
Owner

dai-shi commented Jun 9, 2019

I traced this bug/feature, hoping its a bug. If the initialState does not define the field, during first render it gets skipped over.

Oh, yes. Maybe, it isn't clear enough in README. But, that's the base idea of this library.
All fields be must be provided in createGlobalState and even in createStore.
It's quite different from Redux, and it's more like multiple contexts. Ref: my recent blog

In TypeScript, it will clearly show a compile error, it should probably show a runtime error too for JavaScript.

@dai-shi
Copy link
Owner

dai-shi commented Jun 9, 2019

@vans163

const initialState = {
    user: null,
};

It can be just null. Hope it work for your use case.

@vans163
Copy link
Author

vans163 commented Jun 10, 2019

can be closed, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants