Git Product home page Git Product logo

zustand-learning's Introduction

zustand的学习

以下链接

基本功能的实现

  • createStore
传入函数一个函数(set, get, api) => Store
内部定义函数setStore, getStore, api
store = initialState(setStore, getStore, api)
  • useStore
通过use-sync-external-store插件去定义useStore
  • use-sync-external-store的原理
useSyncExternalStoreWithSelector的原理是基于React的useMemo和useEffect hooks。它接收两个参数:selector和syncFunction。

selector是一个函数,它接收store的状态并返回必须同步的部分。syncFunction是一个函数,它接收必须同步的部分并返回任何需要更新存储的东西。

该hook首先使用useMemo将selector应用于存储的状态以获取必须同步的部分。然后使用useEffect,在必须同步的部分发生变化时调用syncFunction。syncFunction可能会更改存储的状态,例如调用某个API或将数据写入本地存储。

在此过程中,使用React的上下文和钩子来确保存储的状态被正确维护和同步。
  • middleware的使用

很简单, 类似高阶函数的使用

zustand-learning's People

Contributors

alicecooper214 avatar githubstinks avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.