当前位置: 首页 > 知识库问答 >
问题:

前端 - 我再react中使用zustand。我想增加命名空间。一直报错?

尹弘壮
2024-01-20

我再react中使用zustand。我想增加命名空间,代码如下,vscode一直提示image.png

import { create, StateCreator } from "zustand"type AboutState = {  count: number  increase: (n?: number) => void  decrease: () => void}type HomeState = {  n: number  increase: (n?: number) => void  decrease: () => void}type Store = {  about: AboutState  home: HomeState}const createAbout: StateCreator<AboutState> = (set) => {  return {    count: 0,    increase: (n = 1) => set((state) => ({ count: state.count + n })),    decrease: () => set((state) => ({ count: state.count - 1 })),  }}const createHome: StateCreator<HomeState> = (set) => {  return {    n: 0,    increase: (n = 1) => set((state) => ({ n: state.n + n })),    decrease: () => set((state) => ({ n: state.n - 1 })),  }}export const useStore = create<Store>()((...set) => ({  about: createAbout(...set),  home: createHome(...set),}))

共有1个答案

乌骏
2024-01-20

看着感觉你是不是想要下面这种:

type PartialSet<T> = (s: (state: T) => Partial<T>) => unknown;const createAbout = (set: PartialSet<AboutState>) => {  return {    count: 0,    increase: (n = 1) => set((state) => ({ count: state.count + n })),    decrease: () => set((state) => ({ count: state.count - 1 })),  }}const createHome = (set: PartialSet<HomeState>) => {  return {    n: 0,    increase: (n = 1) => set((state) => ({ n: state.n + n })),    decrease: () => set((state) => ({ n: state.n - 1 })),  }}export const useStore = create<Store>()((set) => ({  about: createAbout((innerSet) => set(state => ({ about: {...state.about, ...innerSet(state.about)} }))),  home: createHome((innerSet) => set(state => ({ home: {...state.home, ...innerSet(state.home)} }))),}))// 使用方式:useStore().about.count)useStore().about.increase();
 类似资料:
  • 为什么using指令在包含在匿名命名空间中时表现得好像出现在全局范围?

  • 引入Maven依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId> <version>${shardingsphere.version}</version> </depend

  • 引入 Maven 依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId> <version>${shardingsphere.version}</version> </depe

  • 引入Maven依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId> <version>${shardingsphere.version}</version> </depend

  • 我有一个非常复杂的XML树,它在架构中有几个命名空间。我设法生成了相应的Jaxb(使用eclipse IDE),marshaller/unmarshaller工作正常。 现在,我需要将XML转换为特定格式,因为我需要将其提供给某个系统,并且我没有其他选择。 生成的XML是: 我需要生成的XML 我正在使用Jaxb RI 2.2.6 到目前为止我所做的工作:1。 并从方法返回“” > @使用xmln

  • 我们知道命名空间: 但是什么是全局命名空间呢?