import create from 'zustand'
const useFooBar = create(() => ({ foo: new Map(), bar: new Set() }))
function doSomething() {
// doing something...
// If you want to update some React component that uses `useFooBar`, you have to call setState
// to let React know that an update happened.
// Following React's best practices, you should create a new Map/Set when updating them:
useFooBar.setState((prev) => ({
foo: new Map(prev.foo).set('newKey', 'newValue'),
bar: new Set(prev.bar).add('newKey'),
}))
}
1条答案
按热度按时间kyvafyod1#
是的,你可以做到的。Zustand还接受函数作为
setState
的参数。在这里,您可以参考ZUSTAND文档:https://docs.pmnd.rs/zustand/guides/maps-and-sets-usage