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 { create } from "kosha";

interface CounterStore {
  count: number;
  setCount: (count: number) => void;
}

const useKosha = create<CounterStore>(set => ({
  count: 0,
  setCount: (count: number) => set(state => ({ ...state, count })),
}));

/** Counter Controller */
export const CounterController = () => {
  const { count, setCount } = useKosha();
  return <input type="number" value={count} onChange={e => setCount(Number(e.target.value))} />;
};

/** Counter Display  */
export const CounterDisplay = () => {
  const { count } = useKosha();
  return <div>{count}</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 { create } from "kosha";

interface MyKosha {
  count: number;
  name: string;
  user: {
    name: string;
    age: number;
  };
  setCount: (count: number) => void;
}

export const useMyKosha = create<MyKosha>(set => ({
  count: 0,
  name: "John",
  user: {
    name: "John",
    age: 30,
  },
  setCount: (count: number) => set(state => ({ ...state, count })),
}));

Fork Me on GitHub