🌐
GitHub
github.com › pmndrs › zustand
GitHub - pmndrs/zustand: 🐻 Bear necessities for state management in React
State has to be updated immutably and the set function merges state to help it. import { create } from 'zustand' const useBearStore = create((set) => ({ bears: 0, increasePopulation: () => set((state) => ({ bears: state.bears + 1 })), ...
Starred by 57.9K users
Forked by 2K users
Languages   TypeScript 97.9% | JavaScript 2.1%
🌐
Zustand
zustand.docs.pmnd.rs
Zustand: Introduction
Index of documentation for pmndrs/* libraries
🌐
Refine
refine.dev › home › blog › integrations › how to use zustand
How to use Zustand | Refine
August 2, 2024 - This set function is a function used to manipulate the state in the store. States in zustand can be primitives, objects, or functions. In our above example, we have two states in our store: counter, and incrCounter.
🌐
Medium
medium.com › globant › react-state-management-b0c81e0cbbf3
React State Management — using Zustand | by Chikku George | Globant | Medium
February 15, 2024 - The function get is used to access the state inside actions. The state object of the library store in the example contains three fields: books, which contains an array of book details such as id, name, and author.
🌐
Medium
medium.com › @joris.l › tutorial-zustand-a-simple-and-powerful-state-management-solution-9ad4d06d5334
[Tutorial] Zustand: A Simple and Powerful State Management Solution | by Joris L. | Medium
June 26, 2023 - In this example, we’ve created a cart including computed values to our store. The computed value is a function that returns the total price of the cart. We use the get function from Zustand to access the items state variable and calculate the totalPrice value.
🌐
TkDodo
tkdodo.eu › blog › working-with-zustand
Working with Zustand
November 20, 2022 - One of them that I quickly grew to like was Zustand (opens in a new window). It’s a tiny library (v4.1.4 is 1.1kB Minified + Gzipped) that provides a simple API to create global state stores and subscribe to them via selectors.
🌐
Zustand
zustand.docs.pmnd.rs › reference › hooks › use-store
useStore - Zustand
Each tab will have its own instance of this store. import { createStore } from 'zustand' type CounterState = { count: number } type CounterActions = { increment: () => void } type CounterStore = CounterState & CounterActions const createCounterStore = () => { return createStore<CounterStore>()((set) => ({ count: 0, increment: () => { set((state) => ({ count: state.count + 1 })) }, })) } Next, we'll create a factory function that manages the creation and retrieval of counter stores.
🌐
DEV Community
dev.to › mattlewandowski93 › advanced-global-zustand-store-1i13
Global Zustand Store in React 🌎🐻 - DEV Community
January 27, 2024 - Here we import zustand and immer to create the store. We use the main bears state as the type, and import the substore functions to create the stores.
Find elsewhere
🌐
The Software House
tsh.io › blog › zustand-react
Zustand. React state management set up in a few minutes
February 21, 2023 - Cool thing? Your store is a hook! In the example above, I used the create function taken from Zustand. The callback function passed to create() returns the object and this is exactly where you define the properties you want to store in Zustand.
🌐
TkDodo
tkdodo.eu › blog › zustand-and-react-context
Zustand and React Context
April 14, 2024 - For that, we need to pass the store and the selector to the useStore hook we can get from zustand. This is best abstracted in a custom hook: ... Then, we can use the useBearStore hook like we are used to, and export custom hooks with atomic selectors: ... This is a little bit more code to write than just creating a global store, but it solves all three problems: As the example shows, we can now initialize our store with props, because we create it inside the React Component tree.
🌐
LogRocket
blog.logrocket.com › home › zustand adoption guide: overview, examples, and alternatives
Zustand adoption guide: Overview, examples, and alternatives - LogRocket Blog
August 27, 2024 - Now that you better understand how Zustand improves the state management of React applications, let’s adopt it in an existing tic-tac-toe game built with React. You will install Zustand, set up a game store, and bind it to the components within the /game page.
🌐
DEV Community
dev.to › ricardogesteves › zustand-when-how-and-why-1kpi
Zustand, When, how and why - DEV Community
September 3, 2024 - In this example, we create a store with a bears state and two actions to modify it. The BearCounter and Controls components can then access and modify the state using the useStore hook.
🌐
Frontend Masters
frontendmasters.com › blog › introducing-zustand
Introducing Zustand (State Management) – Frontend Masters Blog
In this post, we’ll first explore what this looks like using React context, and then we’ll examine how Zustand can simplify things while improving performance in the process. There’s a very good argument to be made that React’s context feature was not designed to be a state management library, but that hasn’t stopped many devs from trying. To avoid excessive prop drilling while minimizing external dependencies, developers will often store the state required for a specific part of their UI in context and access it lower in the component tree as needed.
🌐
Js
react-tracked.js.org › person name (zustand)
Tutorial with zustand - Person Name | React Tracked
import * as React from 'react'; import { useState } from 'react'; import create from 'zustand'; type State = { firstName: string; lastName: string; setFirstName: (firstName: string) => void; setLastName: (lastName: string) => void; }; const useStore = create<State>((set) => ({ firstName: 'React', lastName: 'Tracked', setFirstName: (firstName) => set({ firstName }), setLastName: (lastName) => set({ lastName }), })); const EditPerson = () => { const firstName = useStore((state) => state.firstName); const lastName = useStore((state) => state.lastName); const setFirstName = useStore((state) => sta
🌐
DEV Community
dev.to › franklin030601 › using-zustand-with-react-js-9di
Using Zustand with React JS! 🚀 - DEV Community
August 26, 2022 - Then we set the initial value of the properties, in this case the amount property will initially be 40. import create from 'zustand'; interface IBook { amount: number } export const useBookStore = create<IBook>( () => ({ amount: 40 }));
🌐
Codelynx
codelynx.dev › posts › guide-zustand
Zustand : Le guide complet pour gérer ton state en React
Découvre comment utiliser Zustand, le gestionnaire d'état React simple et puissant. Un guide complet avec des exemples concrets en TypeScript.
🌐
Reddit
reddit.com › r/reactjs › how can i make a per-component zustand store?
r/reactjs on Reddit: How can I make a per-component zustand store?
August 12, 2023 -

I made a Cat component using a minimal zustand store:

import { shallow } from "zustand/shallow";
import { CatStoreSchema, useCatStore } from "./CatStore";

export const Cat = () => {
  const selector = (state: CatStoreSchema) => ({
    meows: state.meows,
    incrementMeows: state.incrementMeows,
  });

  const { meows, incrementMeows } = useCatStore(selector, shallow);
  
  return (
    <div>
      <span>Cat meowed {meows} times</span>
      <button onClick={incrementMeows}>Meow!</button> 
    </div>
  );
}

The Store:

import { create } from 'zustand';

export type CatStoreSchema = {
  meows: number;
  incrementMeows: () => void;
};

export const useCatStore = create<CatStoreSchema>((set, get) => ({
  meows: 0,
  incrementMeows: () => set(state => ({ meows: state.meows + 1 })),
}));  

Now, this works great. But what if I want each component to get their own store? Because this is what happens when rendering multiple cat components in a list:

Cat meowed 15 times [Meow!]
Cat meowed 15 times [Meow!]

They share the store. I tried reading about providers but could not get my head around it. How can I ensure unique/separate stores per component here?

UPDATE
I did not mention, in my app <Cat /> will have sub components also needing store data. As it is now, they will select that directly from the store, they are not passed anything. I hope to find a solution where that will continue to work

🌐
Untitled Publication
abeer.hashnode.dev › the-simplest-zustand-tutorial
Learn Zustand state management tool - Untitled Publication
October 4, 2024 - Zustand provides a create function to define your store. Let's walk through a basic example where we store and manipulate a count value.