Skip to content

Suggestion on AtomFamily liked feature #1000

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

You must be logged in to vote

This requires some understanding of React rules, but something like this should be possible.

const todoStoreCache = new Map()
const storeFamilyTodo = (todoId) => {
  let store = todoStoreCache.get(todoId)
  if (!store) {
    store = create(...)
    todoStoreCache.set(todoId, store)
  }
  return store
}

const Component = () => {
  const useStore = storeFamilyTodo('foo');
  // ...
}

You could even create a storeFamily util function.

However, we don't usually do it in zustand. We often create a single store instead:

const useStoreTodos = create((set) => ({
  todos: [],
  // ...
}))

Replies: 1 comment 1 reply

Comment options

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

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