zustand-sliver
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Use Tiamut instead




Zustand Sliver

This package was build to provide zustand with redux-toolkit like api with some opinionated utilities

Usage

Install

npm i --save zustand-sliver

Create a sliver

import { createSliver } from 'zustand-sliver';

const bearSliver = createSliver({
  initialState: {
    firstName: 'Ted',
    lastName: 'Day',
  },
  selectors: {
    fullName(state) {
      return state.firstName + ' ' + state.lastName;
    },
  },
  actions: {
    updateFirstName(state, value: string) {
      state.firstName = value;
    },
  },
});

Create store hook

import { createStore } from 'zustand-sliver';

const store = createStore({
  bear: bearSliver,
});

Use as hook

function MyApp() {
  const firstName = store((state) => state.bear.firstName);
  const fullName = store.use.bear.fullName();
  const updateFirstName = store.actions.bear.updateFirstName;

  function changeFirstName() {
    updateFirstName('Ted ' + new Date().valueOf());
  }

  return (
    <div>
      <div>firstName: {firstName}</div>
      <div>fullName: {fullName}</div>
      <button onClick={changeFirstName}>Update Name</button>
    </div>
  );
}

Readme

Keywords

none

Package Sidebar

Install

npm i zustand-sliver

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

29.1 kB

Total Files

16

Last publish

Collaborators

  • ahdng