linjiajian999 / zustand-valtio

A sweet combination of Zustand and Valtio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zustand-valtio

CI npm size discord

A sweet combination of Zustand and Valtio

Install

npm install zustand zustand-valtio valtio

Usage

import { createWithProxy } from 'zustand-valtio';

const [useCounterState, counterState] = createWithProxy({ count: 0 });

const Counter = () => {
  const count = useCounterState((state) => state.count);
  const inc = () => ++counterState.count;
  return (
    <div>
      {count} <button onClick={inc}>+1</button>
    </div>
  );
};

But, why?

Zustand has immer middleware to update state mutably. Valtio has the same capability. Isn't the combination is sweet?

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 yarn run examples:01_typescript

and open http://localhost:8080 in your web browser.

You can also try them in codesandbox.io: 01

Tweets

About

A sweet combination of Zustand and Valtio

License:MIT License


Languages

Language:JavaScript 53.9%Language:TypeScript 46.1%