use-share-state
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

React Shared State Hook 🔗

React hook for state synchronization between (windows, tabs).

This hook is based on a broadcast-channel library that works in New Browsers, Old Browsers, WebWorkers and NodeJs.

Installation

$ npm install use-share-state

Usage

import useShareState from "use-share-state";

const App = () => {
  const [state, setState] = useShareState(0);

  const increment = useCallback(() => {
    setState(state + 1);
  }, [state]);

  const decrement = useCallback(() => {
    setState(state - 1);
  }, [state]);

  return (
    <div className="App">
      Counter: {state}
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
    </div>
  );
}

License

MIT © Muslim Guseinov

Package Sidebar

Install

npm i use-share-state

Weekly Downloads

2

Version

2.0.1

License

MIT

Unpacked Size

5.48 kB

Total Files

6

Last publish

Collaborators

  • gus3inov