zustand 带着 zustand-vue / zustand-pub,它们来了!!!

文章介绍了Zustand,一个轻巧灵活的状态管理库,作为Redux和ReactContext的替代方案。Zustand基于React思想采用不可变更新,避免了无谓的组件渲染,提高了性能。与MobX相比,Zustand更新状态的方式更像React。文章还提供了React和Vue的上手教程,展示了如何简单地初始化Store和绑定组件,并强调了Zustand在跨应用、跨框架状态管理和共享方面的优势。
摘要由CSDN通过智能技术生成

如果有兴趣了解更多用法及 api ,点击此处解锁中文文档

前言

是不是觉得 Redux 很难用?想用 Context 代替,但是你知道吗,Context 也有个很大的缺点:

  • context value发生变化时,所有用到这个context的组件都会被重新渲染,即使 component 需要的 state 可能根本沒有变动。基于 context 维护的模块越多,影响范围越大, 某些情况下会导致页面明显卡顿。
  • 另外,它依赖 Context Provider 包裹你的应用程序。

那么试试 zustand 吧,当然你可以选择 mobx,
zustand 与 mobx 最大的差别在于:

  • zustand 的状态更新遵循 react 思想,采用自然不可变更新, 而 mobx 类似 vue,基于数据劫持直接修改状态本身。
  • 体现在开发层面最直观的差异就是:
    • zustand 状态更新,新状态覆盖旧状态
    state = {a: 1}
    
    update(){
      state = {a: 2} 
    }
    
    • mobx 状态更新,直接修改属性值
    state = {a: 1}
    
    update(){
      state.a++
    }
    

对比其他状态管理框架

为什么是 zustand 而不是 redux?

为什么是 zustand 而不是 react Context?

  • 不依赖 react 上下文,引用更加灵活
  • 当状态发生变化时 重新渲染的组件更少
  • 集中的、基于操作的状态管理

为什么是 zustand-vue 而不是 pinia?

  • 基于不可变状态进行更新, store 更新操作相对更加可控
  • composition api 作为消费状态的主要手段,而不是 Vue.use 全局注入

最重要的是

  • 基于 zustand-pub 可以为 Iframe、微前端、Module Fedetation、模块化、组件化 等业务场景,提供 跨应用、跨框架状态管理状态共享 能力。

React 上手三部曲

Step 1: 安装

npm install zustand # or yarn add zustand

Step 2: Store 初始化

创建的 store 是一个 hook,你可以放任何东西到里面:基础变量,对象、函数,状态必须不可改变地更新,set 函数合并状态以实现状态更新。

import { create } from 'zustand'

const useBearStore = create((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
}))

Step 3: Store 绑定组件,就完成了!

可以在任何地方使用钩子,不需要提供 provider
基于 selector 获取您的目标状态,组件将在状态更改时重新渲染。

选择目标状态:bears
function BearCounter() {
  const bears = useBearStore((state) => state.bears)
  return <h1>{bears} around here ...</h1>
}
更新目标状态:bears
function Controls() {
  const increasePopulation = useBearStore((state) => state.increasePopulation)
  return <button onClick={increasePopulation}>one up</button>
}

Vue 上手三部曲

什么,你还想试试在 Vue 中使用?那么 Step1 与 Step2 基本一致,不同的是 Step3 (Store 绑定组件):

Step 1: 安装

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

Step 2: Store 初始化

创建的 store 是一个 hook,你可以放任何东西到里面:基础变量,对象、函数,状态必须不可改变地更新,set 函数合并状态以实现状态更新。

import create from "zustand-vue";

const useBearStore = create((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
}))

export default useBearStore

Step 3: Store 绑定组件,就完成了!

基于 selector 获取您的目标状态,组件将在状态更改时重新渲染。

Store 绑定组件在 vue3vue2 中有所不同。

<template>
  <div>store.bears: {{ bears }}</div>
  <button @click="increasePopulation">increasePopulation</button>
  <button @click="removeAllBears">removeAllBears</button>
</template>

<script>
import useBearStore from "./store";

const increasePopulation = useBearStore((state) => state.increasePopulation);
const removeAllBears = useBearStore((state) => state.removeAllBears);

export default {
  data() {
    return {
      store: useBearStore(),
      bears: useBearStore((state) => state.bears),
    };
  },
  methods: {
    increasePopulation,
    removeAllBears,
  },
};

更多生态及能力说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值