🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

better-zustand-selector

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-zustand-selector

A better way to use selectors with zustand

1.0.6
latest
npm
Version published
Weekly downloads
2
Maintainers
0
Weekly downloads
 
Created
Source

Better Zustand Selector

A better way to use selectors with zustand that allows you to access multiple states from the store at once without causing unnecessary re-renders.

Installation

npm install better-zustand-selector

pnpm install better-zustand-selector

yarn add better-zustand-selector

Demo

Try it out on Code Sandbox

Usage

import { create } from "zustand";
import { createSelector } from "better-zustand-selector";

const counterStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
}));
const useCounterStore = createSelector(counterStore);

const Counter = () => {
  const { count, increment } = useCounterStore("count", "increment");
  return <button onClick={increment}>{count}</button>;
};

Partial Selectors

You can also retrieve partial states from the store without causing re-renders when the unaccessed states change. For example, if you only want to access the count state from the store, you can use the useCounterStore hook like this:

const { count } = useCounterStore("count");

Keywords

zustand

FAQs

Package last updated on 24 Jun 2024

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