Next.js Example

Craft your next amazing library using

Harness the full potential of React 18 Server Components!

A tiny yet powerful store for modern react libraries.

NPM Bundle Size

Basic Example: Synced counters

All components below share the same state without any wrapper.

Counter Controller 1

Counter Display 1

0

Counter Controller 2

Counter Display 2

0
ShowHideΒ Code
import { useRGS } from "r18gs";

const RGS_COUNTER_KEY = "counter";

/** Counter Controller */
export const CounterController = () => {
  const [counter, setCounter] = useRGS(RGS_COUNTER_KEY, 0);
  return <input type="number" value={counter} onChange={e => setCounter(Number(e.target.value))} />;
};

/** Counter Display  */
export const CounterDisplay = () => {
  const [counter] = useRGS(RGS_COUNTER_KEY, 0);
  return <div>{counter}</div>;
};

Example with Selectors

My name is John

Updates only when name changes. renderCount = 1

Counter With Selectors

Rerender is triggered by RGS only when count changes.

Count: 0

Render Count: 1

Counter Without Selectors

Rerender is triggered every time the state changes.

Count: 0

Render Count: 1

UserData

renderCount = 1

Name: John

Age: 30

ShowHideΒ Code
import { useRGS } from "r18gs";

interface MyStore {
  count: number;
  name: string;
  user: {
    name: string;
    age: number;
  };
}

export const useStore = (includeRegExp?: RegExp | null | 0, excludeRegExp?: RegExp) =>
  useRGS<MyStore>(
    "my-store-with-selectors",
    {
      count: 0,
      name: "John",
      user: {
        name: "John",
        age: 30,
      },
    },
    includeRegExp,
    excludeRegExp,
  );

Fork Me on GitHub