🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

zustand

Package Overview
Dependencies
Maintainers
3
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand

🐻 Bear necessities for state management in React

5.0.4
latest
Source
npm
Version published
Weekly downloads
7.4M
5.46%
Maintainers
3
Weekly downloads
 
Created

What is zustand?

Zustand is a state management library for React and other JavaScript applications. It provides a simple and flexible way to create and manage global state without the complexity of traditional solutions like Redux. Zustand uses a hook-based API to allow components to subscribe to state changes and define actions for updating the state.

What are zustand's main functionalities?

Creating a store

This code sample demonstrates how to create a store with Zustand. The store has a state with a 'fishes' property and an 'addFish' action to increment the number of fishes.

import create from 'zustand';

const useStore = create(set => ({
  fishes: 0,
  addFish: () => set(state => ({ fishes: state.fishes + 1 }))
}));

Subscribing to state changes

This code sample shows how a React component can subscribe to state changes. The 'FishCounter' component uses the 'useStore' hook to access the number of fishes from the store's state.

import React from 'react';
import useStore from './store';

function FishCounter() {
  const fishes = useStore(state => state.fishes);
  return <div>{fishes} fishes</div>;
}

Updating the state

This code sample illustrates how to update the state using an action defined in the store. The 'AddFishButton' component gets the 'addFish' action from the store and uses it as an onClick event handler.

import React from 'react';
import useStore from './store';

function AddFishButton() {
  const addFish = useStore(state => state.addFish);
  return <button onClick={addFish}>Add a fish</button>;
}

Other packages similar to zustand

Keywords

react

FAQs

Package last updated on 01 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts