react-localstorage-layer

0.9.0 • Public • Published

React LocalStorage Layer

This package makes localStorage state available to your app. It maintains a shared context that syncs localStorage state updates

  1. Wrap your app with LocalStorageContextProvider:
function () {
    return (
        <LocalStorageContextProvider>
            <App />
        </LocalStorageContextProvider>
    )
}
  1. Use useLocalStorageState to get or set localStorage state
function Example() {
    const [n, setN] = useLocalStorageState('n', 0);

    return (
        <div>
            <button onClick={() => setN(n+1)}>{n}</button>
        </div>
    )
}
  1. That's mostly it! Now you can use useLocalStorageState in any other component. It will stay in sync as you dispatch updates, even across tabs

Readme

Keywords

none

Package Sidebar

Install

npm i react-localstorage-layer

Weekly Downloads

2

Version

0.9.0

License

MIT

Unpacked Size

8.16 kB

Total Files

4

Last publish

Collaborators

  • lunaroyster