Skip to content

How do I subscribe to a store in Nextjs when store is wrapped in a React Context. #2534

Closed Answered by dbritto-dev
KamaniBhavin asked this question in Q&A
Discussion options

You must be logged in to vote

@KamaniBhavin here you go:

export function useStore<T = IStore, U = T>(selector: (state: T) => U): [U, StoreApi<T>] {
  const store = useContext(StoreContext);

  if (store === undefined) {
    throw new Error(`useStore must be used within StoreProvider`);
  }

  return [useStore(store, selector), store]
}

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by KamaniBhavin
Comment options

You must be logged in to vote
4 replies
@dbritto-dev
Comment options

@KamaniBhavin
Comment options

@dbritto-dev
Comment options

@KamaniBhavin
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants