本页提供了 Zustand API 表面和核心功能的全面参考。它涵盖了 vanilla JavaScript 核心和 React 集成,让您全面了解用于管理应用程序状态的可用工具。有关中间件的具体信息,请参阅中间件系统。
来源: src/vanilla.ts43-100 src/react.ts17-64 src/traditional.ts23-90
createStoreCreates a vanilla (framework-agnostic) store that can be used in any JavaScript environment.
签名
Returns: A store API object with getState, setState, subscribe, and getInitialState methods.
The createStore function is the foundation of all Zustand stores. It creates a store instance containing state and providing methods to interact with that state.
Sources: src/vanilla.ts43-100
createCreates a React hook with store API methods attached.
签名
Returns: A React hook that can access the store state with optional selectors.
The create function combines store creation with React integration, providing a hook that components can use to access state.
Sources: src/react.ts44-64
createWithEqualityFnCreates a React hook with store API methods attached and a custom equality function.
签名
Returns: A React hook with custom equality checking to control re-renders.
This function extends create with the ability to customize when components re-render based on state changes.
Sources: src/traditional.ts57-90
useStoreConnects a vanilla store to React components.
签名
Returns: The selected state from the store.
The useStore hook allows React components to access state from vanilla stores that were created with createStore.
Sources: src/react.ts17-37
useStoreWithEqualityFnConnects a vanilla store to React components with custom equality checking.
签名
Returns: The selected state from the store, with re-renders controlled by the equality function.
This hook extends useStore with custom equality checking for optimized rendering.
Sources: src/traditional.ts23-47
useShallowCreates a selector with built-in shallow equality checking for use with Zustand stores.
签名
Returns: A new selector function that uses shallow equality to optimize re-renders.
Sources: src/react/shallow.ts4-12
The StoreApi interface defines the core methods available on all Zustand stores
Sources: src/vanilla.ts9-14
setStateUpdates the store state.
partial: New state object or function that returns a state updatereplace: When true, completely replaces state instead of merginggetStateGets the current store state.
subscribeSubscribes to state changes.
Returns an unsubscribe function.
getInitialStateGets the initial store state.
Sources: src/vanilla.ts60-97 src/react.ts30-36
Sources: src/vanilla.ts9-41 src/react.ts39-42 src/traditional.ts49-56
StoreApi<T>Core interface for all Zustand stores.
Sources: src/vanilla.ts9-14
StateCreator<T>Type for functions that create store state.
Sources: src/vanilla.ts28-37
UseBoundStore<S>Type for React hooks returned by create().
Sources: src/react.ts39-42
UseBoundStoreWithEqualityFn<S>Type for React hooks returned by createWithEqualityFn().
Sources: src/traditional.ts49-55
createSources: src/vanilla.ts66-81 tests/basic.test.tsx130-163
Zustand's middleware system allows extending store functionality. Each middleware wraps the store creator and can add functionality or modify behavior. For more details on specific middleware, see Middleware System.
刷新此 Wiki
最后索引时间2025 年 4 月 18 日(4d3a01)