Socket
Socket
Sign inDemoInstall

zustand-slice

Package Overview
Dependencies
9
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    zustand-slice

A zustand middleware that provides an API like createSlice from Redux toolkit.


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

zustand-slice

A zustand middleware that provides an API like createSlice from Redux toolkit.

Install

npm install zustand-slice

or

yarn add zustand-slice

Usage

Create a store using slice middleware.

import create from 'zustand';
import slice from 'zustand-slice';

const useStore = create(
  slice({
    initialState: {
      bears: 0
    },
    reducers: {
      increasePopulation(state) {
        return {
          bears: state.bears + 1
        };
      },
      removeAllBears() {
        return {
          bears: 0
        };
      }
    }
  })
);

And use it as usual.

function BearCounter() {
  const bears = useStore(state => state.bears);
  return <h1>{bears} around here ...</h1>;
}

function Controls() {
  const increasePopulation = useStore(state => state.increasePopulation);
  return <button onClick={increasePopulation}>one up</button>;
}

Usage with typescript

By default, types will be inferred from initialState object and reducers payload, so there is no need to type the whole store explicitly.

License

MIT

Keywords

FAQs

Last updated on 30 Dec 2021

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc