Skip to content

Compared with zustand #1

@ysmood

Description

@ysmood

Here's the example code of zustand from link:

import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'

type State = {
  count: number
}

type Actions = {
  increment: (qty: number) => void
  decrement: (qty: number) => void
}

export const useCountStore = create<State & Actions>()(
  immer((set) => ({
    count: 0,
    increment: (qty: number) =>
      set((state) => {
        state.count += qty
      }),
    decrement: (qty: number) =>
      set((state) => {
        state.count -= qty
      }),
  })),
)

Same logic and type safety in stalo:

import { create } from "stalo/lib/immer"

export const [useStore, setStore] = create({
  count: 1,
})

export const increment = (n: number) => setStore((s) => { s.count += n })
export const decrement = (n: number) => setStore((s) => { s.count -= n })

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions