A shared state library for JavaScript
pnpm install @shared-state/core
import { createSharedState } from "@shared-state/core";
const counter = createSharedState(0);
counter.get(); // Get state
counter.set((count) => count + 1); // Update state
counter.subscribe(({ previousState, nextState }) =>
console.log(previousState, nextState)
); // Subscribe state
import { createSharedState } from "@shared-state/core";
import { useSharedState } from "@shared-state/react";
const CounterState = createSharedState(0);
function Counter() {
const [count, setCount] = useSharedState(CounterState);
return (
<button onClick={() => setCount((count) => count + 1)}>{count}</button>
);
}