Playground

useLocalStorage

A JSON value kept in localStorage, shaped like useState, that stays in sync across tabs.

  • No DOM output. Storage failures (blocked, full) fall back to memory and do not throw.
  • Other tabs receive changes through the storage event.

Import

import '@monoline/css';
import {useLocalStorage} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-local-storage

Usage

import {Button, useLocalStorage} from '@monoline/react';

function CountDemo() {
  const [count, setCount] = useLocalStorage('monoline-demo-count', 0);
  return (
    <div>
      <Button variant="line" onClick={() => setCount((n) => n + 1)}>Count is {count}</Button>
      <p>Saved in this browser. Open this page in another tab and click there to see it sync.</p>
    </div>
  );
}

<CountDemo />

Examples

Persisted counter

Behaviour

  • No DOM output. Storage failures (blocked, full) fall back to memory and do not throw.
  • Other tabs receive changes through the storage event.