Skip to content

lmarklil/shared-state

Repository files navigation

Shared State

A shared state library for JavaScript

Installation

pnpm install @shared-state/core

Quick Start

Basic

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

Using with React function component

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>
  );
}

About

A shared state tools for JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published