kingdee-cosmic-clikingdee-cosmic-cli
首页
指南
  • 金蝶云苍穹社区
  • 资源市集
  • KDesign
  • CHATUI X
作者博客
首页
指南
  • 金蝶云苍穹社区
  • 资源市集
  • KDesign
  • CHATUI X
作者博客
  • 起步
    • 介绍
    • 安装与创建
    • 卸载与升级
  • 全局配置
  • 开发事项
    • 介绍
    • 苍穹预览模式
    • RAM模式
  • 生产与产物分析事项
  • 资源拉取策略分析
  • 数据请求策略
    • 策略设计
    • Mock服务
  • React18 起步工程
    • 介绍
    • 接口请求书写
    • 状态管理
    • 国际化
  • Vue3 起步工程
    • 介绍
    • 接口请求书写
    • 状态管理
    • 国际化
  • 主题色切换
  • 图标方案
  • 规范与温馨提示
    • Eslint
    • 工程与编码规范
    • 温馨提示
  • 未来计划与感谢 📅

背景

引用状态管理工具是为了解决 update 数据推送监听的问题,目前采用的库为 Zustand。

Zustand 实例化方式

因为前面提到过的变量隔离问题,所以 Zustand 必须也在类的声明中实例化。

export class InitComponent {
  model: ModelType;
  globalRoot: ReactDOM.Root | null = null;
  zustandStore: ZustandStore;
  constructor(model: ModelType) {
    this.model = model;
    this.zustandStore = zustandStore();
    /* 用import("").then语法配合代码分割+css独立文件打包,可以让打包后的js文件自动拉取css文件 */
    import("./components/context/AppGlobal").then((AppModule) => {
      const AppGlobal = AppModule.default;

      this.globalRoot = ReactDOM.createRoot(this.model.dom as HTMLElement);
      this.globalRoot.render(
        <AppGlobal
          model={this.model}
          zustandStore={this.zustandStore}
        ></AppGlobal>
      );
    });
  }

  init(props: ReturnDataType) {
    console.log("-----init", this.model, props);
    saveConfig(props, this.zustandStore);
  }

  update(props: ReturnDataType) {
    console.log("-----update", this.model, props);
    saveConfig(props, this.zustandStore);
    this.zustandStore.useGlobalStore.getState().setAjaxData(props);
  }

  destoryed() {
    console.log("-----destroyed", this.model);
    if (this.globalRoot) {
      this.globalRoot.unmount();
      this.globalRoot = null;
    }
  }
}

使用

在 tsx 组件中:

// App.tsx
import React, { useContext, useState } from "react";
import { AppContext } from "@/components/index";

const App: React.FC = () => {
  const { zustandStore } = useContext(AppContext);
  const { value, increment } = zustandStore.useGlobalStore();

  return (
    <div>
      <h3>zustand数据演示</h3>
      <div className={Style.itemWrap}>
        <span>
          zustand数据:
          {value}
        </span>
        <button
          onClick={() => {
            increment();
          }}
        >
          点我zustand数据+1
        </button>
      </div>
    </div>
  );
};

export default App;

遗憾

由于 Zustand 是在类中实例化的,并且通过 Context 传递到各个组件中。所以在其他 .ts 文件中就无法获取到它的实例,所以无法在其他文件中使用 Zustand 的状态和方法,只能在组件中使用。

最近更新:: 2025/3/28 10:44
Contributors: 庞囧, pangzong
Prev
接口请求书写
Next
国际化