跳到主要内容

Map 和 Set 的使用

我们需要将 MapSet 包装在一个对象内。当我们希望它的更新被响应(例如,在 React 中), 我们可以通过调用 setState 来实现:

import { create } from 'zustand';

export const useBoundStore = create(() => ({
foo: new Map(),
bar: new Set(),
}));

function doSomething() {
useBoundStore.setState((state) => ({
foo: new Map(state.foo).set('newKey', 'newValue'),
bar: new Set(state.bar).add('newKey'),
}));
}