请问下大佬,zustand中slice 有相同的action的名称,在合并为store的时候,会覆盖吗?请问是需要在slice定义的时候就区分开来吗,还是指的有区分的方法呢?
import { create, StateCreator } from "zustand";
import { subscribeWithSelector } from "zustand/middleware";
export interface ASlice {
aState: string,
action1: () => void,
action2: () => void
}
export interface BSlice {
bState: string,
action1: () => void,
action2: () => void
}
export const createASlice: StateCreator<ASlice & BSlice, [], [["zustand/subscribeWithSelector", never]], ASlice> = (set, get) => ({
aState: 'a',
action1: () => {
set({ aState: 'a' })
},
action2: () => {
// get can obtain the variables or methods defined in this Slice
set({ aState: 'b' })
}
});
export const createBSlice: StateCreator<ASlice & BSlice, [], [["zustand/subscribeWithSelector", never]], BSlice> = (set, get) => ({
bState: 'b',
action1: () => {
},
action2: () => {
// get can obtain the variables or methods defined in this Slice
const { aState } = get()
console.log(aState)
}
});
// Zustand store
export const useStore = create<ASlice & BSlice>()(subscribeWithSelector((...params) => ({
...createASlice(...params),
...createBSlice(...params)
})));
用不同的命名(推荐):
用命名空间:
用的时候: