-
I am currently in transition from Recoil to Zustand on my project. The Recoil is excellent, but the layer of the complexities and difficulties in maintenances exponentially grows as the project becomes larger. Zustand...is on the other hand, just light-weighted, but it just works. No wonder Zustand is so popular. My current challenge in Zustand is the SHARABLE local state. I was achieving this with For Example: // such as in Recoil
// initial atomFamilyTodo value is `false`
const todo = useRecoilValue(atomFamilyTodo(todoID))
// This creates:
// ...
// todo with ID 1 false
// todo with ID 2 false
// todo with ID 3 false
// ...
// state is isolated with the given ID As my knowledge about Zustand is not so deep yet, the only solutions I can think of are
Is there an option or suggestion to achieve the Sharable local state in Zustand? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 However, we don't usually do it in zustand. We often create a single store instead: const useStoreTodos = create((set) => ({
todos: [],
// ...
})) |
Beta Was this translation helpful? Give feedback.
This requires some understanding of React rules, but something like this should be possible.
You could even create a
storeFamily
util function.However, we don't usually do it in zustand. We often create a single store instead: