Skip to content

How to use vanilla Zustand store with React Tracked? #989

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

You must be logged in to vote

This is a bit tricky and not 100% recommended hook pattern, but something like this should work:

import { createContext, useContext } from 'react'
import { createStore, useStore } from 'zustand'
import { createTrackedSelector } from 'react-tracked'

const store = createStore(...) // vanilla store without hooks

const StoreContext = createContext()

const App = () => (
  <StoreContext.Provider value={store}>
    ...
  </StoreContext.Provider>
)

const Component = () => {
  const store = useContext(StoreContext)
  const useTrackedStore = useMemo(() => createTrackedSelector(
    (selector) => useStore(store, selector)
  ), [store])
  const state = useTrackedStore()
  ...
}

(...Not exactly su…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@daphdev
Comment options

Answer selected by daphdev
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