A sweet combination of Zustand and Valtio
npm install zustand zustand-valtio valtio
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>
);
};
Zustand has immer
middleware to update state mutably.
Valtio has the same capability. Isn't the combination is sweet?
The examples folder contains working examples. You can run one of them with
PORT=8080 yarn run examples:01_counter
and open http://localhost:8080 in your web browser.