zustand-pub 中文文档教程

发布于 2年前 浏览 18 项目主页 更新于 2年前

zustand-pub

构建大小版本

????基于 zustand for React/Vue 的跨应用/跨框架状态管理和共享。

官方文档

能力

适合模块化、组件化、微前端等业务场景,提供跨应用程序和框架的状态管理和共享功能。

安装

npm install zustand-pub # or yarn add zustand-pub

使用

步骤1:初始化状态隔离容器pubStore(App A)

import PubStore from 'zustand-pub'

const pubStore = new PubStore('key')

步骤2:向隔离容器pubStore填充数据< code>platformStore(App A)

//react
import create from "zustand";

// vue
// import create from "zustand-vue";

interface IState {
  appInfo: {
    name: string
  }
}

interface IAction {
  setAppName: (val: string) => void
}

const platformStore = pubStore.defineStore<IState & IAction>('platformStore', (set) => ({
  appInfo: { name: '' },
  setAppName(val: string) {
    set({
      appInfo: {
        name: val
      }
    })
  }
}))

const usePlatformStore = create(platformStore)

返回值usePlatformStorehook,可以通过state selector获取对应的状态

// react
function AppA() {
  const name = usePlatformStore((state) => state.appInfo.name);
  const setAppName = usePlatformStore((state) => state.setAppName);
  return <div>{name}</div>
}

第3步:获取隔离容器 pubStore 下的 platformStore 并绑定组件(App B)

interface IState {
  appInfo: {
    name: string
  }
}

interface IAction {
  setAppName: (val: string) => void
}

// react
import PubStore from "zustand-pub";
import create from "zustand";
const pubStore = new PubStore('key')

// vue
// import PubStore from "zustand-pub";
// import create from "zustand-vue";
// const pubStore = new PubStore('key')


const store = pubStore.getStore<IState & IAction>("platformStore");
const usePlatformStore = create(store || {});

// react
function AppB() {
  const name = usePlatformStore((state) => state.appInfo.name);
  const setAppName = usePlatformStore((state) => state.setAppName);
  return <div>{name}</div>
}

:::tip Vue绑定组件的用法 :::

API

PubStore(str)

用于创建状态隔离容器,不同隔离容器内的数据key可以重名且互不影响

:::tip 在同一应用程序中,key 保持不变,并且 pubStore 返回不变 :::

const pubStore = new PubStore() 

DefineStore(key,fn)

用于将数据填充到隔离容器中

:::tip 在同一个应用中,key不变,定义的store会按照加载的顺序合并,

defineStore(key,()=>({ a:1})) DefineStore(key,()=>({b:2})) 的工作方式类似于 defineStore(key,()=>({a:1,b:2 })) :::

参数描述类型
关键数据 唯一标识符字符串
fn回调(set, get) => Object
interface IStore {
  ...
}

// usePlatformStore is `hook`, and the corresponding state can be obtained through state `selector`
const usePlatformStore = pubStore.defineStore<IStore>('platformStore', (set, get) => ({}))

getStore(key)

用于从隔离容器中获取数据

参数描述类型
key数据唯一标识符字符串
const platformStore = pubStore.getStore("platformStore");

返回值 platformStore 可用于创建 hook

import create from "zustand";

//vue
// import create from "zustand-vue";

const usePlatformStore = create(platformStore || {});

zustand-pub

Build SizeVersion

???? Cross-Application/Cross-Framework State Management And Sharing based on zustand for React/Vue.

Official Document

Ability

It is suitable for business scenarios such as modularization, componentization, and micro-front-end, and provides state management and sharing capabilities across applications and frameworks。

Install

npm install zustand-pub # or yarn add zustand-pub

Usage

Step 1: Initialize state isolation container pubStore (App A)

import PubStore from 'zustand-pub'

const pubStore = new PubStore('key')

Step 2: Fill the isolation container pubStore with data platformStore (App A)

//react
import create from "zustand";

// vue
// import create from "zustand-vue";

interface IState {
  appInfo: {
    name: string
  }
}

interface IAction {
  setAppName: (val: string) => void
}

const platformStore = pubStore.defineStore<IState & IAction>('platformStore', (set) => ({
  appInfo: { name: '' },
  setAppName(val: string) {
    set({
      appInfo: {
        name: val
      }
    })
  }
}))

const usePlatformStore = create(platformStore)

return value usePlatformStore is hook, you can get the corresponding state through state selector

// react
function AppA() {
  const name = usePlatformStore((state) => state.appInfo.name);
  const setAppName = usePlatformStore((state) => state.setAppName);
  return <div>{name}</div>
}

Step 3: Get the platformStore under the isolated container pubStore and bind the Component (App B)

interface IState {
  appInfo: {
    name: string
  }
}

interface IAction {
  setAppName: (val: string) => void
}

// react
import PubStore from "zustand-pub";
import create from "zustand";
const pubStore = new PubStore('key')

// vue
// import PubStore from "zustand-pub";
// import create from "zustand-vue";
// const pubStore = new PubStore('key')


const store = pubStore.getStore<IState & IAction>("platformStore");
const usePlatformStore = create(store || {});

// react
function AppB() {
  const name = usePlatformStore((state) => state.appInfo.name);
  const setAppName = usePlatformStore((state) => state.setAppName);
  return <div>{name}</div>
}

:::tip The Usage of Vue to bind Component :::

API

PubStore(str)

Used to create state isolation containers, the data key inside different isolation containers can have the same name and do not affect each other

:::tip In the same application, key is unchanged and the pubStore is returned unchanged :::

const pubStore = new PubStore() 

defineStore(key,fn)

Used to fill data into isolated containers

:::tip In the same application, key is unchanged and the defined store will be merged in the order of loading

that is defineStore(key,()=>({a:1})) defineStore(key,()=>({b:2})) works like defineStore(key,()=>({a:1,b:2})) :::

ParameterDescType
keydata unique identifierstring
fncallback(set, get) => Object
interface IStore {
  ...
}

// usePlatformStore is `hook`, and the corresponding state can be obtained through state `selector`
const usePlatformStore = pubStore.defineStore<IStore>('platformStore', (set, get) => ({}))

getStore(key)

Used to fetch data from isolated containers

ParameterDescType
keydata unique identifierstring
const platformStore = pubStore.getStore("platformStore");

Return value platformStore can be used to create hook

import create from "zustand";

//vue
// import create from "zustand-vue";

const usePlatformStore = create(platformStore || {});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文