Skip to content

External api for async inital state #676

Answered by dai-shi
TorbjornHoltmon asked this question in Q&A
Discussion options

You must be logged in to vote

The initial value has to be something in sync.

const useStore = create(set => ({
  bears: null,
  increasePopulation: () => set(state => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 })
}))

// Fetch that gets the current bears from api
getBearsAsync().then((bears) => useStore.setState({ bears }))

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@kenneropia
Comment options

@dai-shi
Comment options

@dai-shi
Comment options

@kenneropia
Comment options

Answer selected by TorbjornHoltmon
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants