Skip to content

Best way to integrate custom hook into store #1308

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

You must be logged in to vote

Store definition is pure JS. You want to create a new hook from the store hook.

const useCustomFetch = (path) => { ... }

const useFishStore = create((set) => ({
  fishies: {},
  setFishes: (fishes) => set({ fishes }),
}))

const useFishFetch = () => {
  const customFetch = useCustomFetch()
  const setFishes = useFishStore((state) => state.setFishes)
  return async (pond) => {
    const response = await customFetch(pond)
    set({ fishies: await response.json() })
  }
}

You can feed customFetch into the store action.

const useCustomFetch = (path) => { ... }

const useFishStore = create((set) => ({
  fishies: {},
  fetch: async (pond, customFetch) => {
    const response = await customFetch(

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@merriman-xyz
Comment options

@TomlDev
Comment options

Answer selected by merriman-xyz
Comment options

You must be logged in to vote
0 replies
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
5 participants