Package Exports
- zustand-utils
- zustand-utils/es/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (zustand-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
zustand-utils
Introduction
Some utils for zustand
createContext
A replacement createContext from zustand/context that is deprecated in v4 and will be removed in v5. (Discussion: #1276)
import create from 'zustand'
import createContext from 'zustand/context'
const { Provider, useStore } = createContext()
const createStore = () => create(...)
const App = () => (
<Provider createStore={createStore}>
...
</Provider>
)
const Component = () => {
const state = useStore()
const slice = useStore(selector)
...Usage
createContext
createContext usage in real components
Migration from zustand-v3-create-context.md
import create from "zustand";
import createContext from "zustand/context";
// Best practice: You can move the below createContext() and createStore to a separate file(store.js) and import the Provider, useStore here/wherever you need.
const { Provider, useStore } = createContext();
const createStore = () =>
create((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 })
}));
const Button = () => {
return (
{/** store() - This will create a store for each time using the Button component instead of using one store for all components **/}
<Provider createStore={createStore}>
<ButtonChild />
</Provider>
);
};
const ButtonChild = () => {
const state = useStore();
return (
<div>
{state.bears}
<button
onClick={() => {
state.increasePopulation();
}}
>
+
</button>
</div>
);
};
export default function App() {
return (
<div className="App">
<Button />
<Button />
</div>
);
}createContext usage with initialization from props
Migration from zustand-v3-create-context.md
import create from 'zustand';
import createContext from 'zustand/context';
const { Provider, useStore } = createContext();
export default function App({ initialBears }) {
return (
<Provider
createStore={() =>
create((set) => ({
bears: initialBears,
increase: () => set((state) => ({ bears: state.bears + 1 })),
}))
}
>
<Button />
</Provider>
);
}Refactor app store to a component store with createContext
a most usage of createContext is refactoring an app to a component.
Here's progress:
- Create an App without context :
// store.ts
import create from 'zustand';
export const useStore = create((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 }),
}));components in app use useStore to consume store:
// Component.ts
import { useStore } from './store';
const ButtonChild = () => {
const state = useStore();
return (
<div>
{state.bears}
<button
onClick={() => {
state.increasePopulation();
}}
>
+
</button>
</div>
);
};
export default ButtonChild;- Oneday, the app needs to be converted to a component(like react-flow) to reuse in another app.
with createContext, what needs to do is just wrapper the App with createContext, and don't need to refactor any code in children components.
It become a component, can be used in any other app.
// store.ts
import create from "zustand";
+ const createStore = ()=> create((set) => ({
- export const useStore = create((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 })
}));
+ const { Provider, useStore } = createContext();
+ export { Provider, useStore , createStore }// Wrapper.tsx
import { createStore, Provider } from './store';
const Wrapper = () => {
return (
<Provider createStore={createStore}>
<ButtonChild />
</Provider>
);
};