create-global-hook

Takes an aribtrary React hook and makes it globally stateful.

Usage no npm install needed!

<script type="module">
  import createGlobalHook from 'https://cdn.skypack.dev/create-global-hook';
</script>

README

Takes an aribtrary React hook and makes it globally stateful.

It's like 12 lines of code, so you should probably just copy the source rather than include this in your package.json. 🤷‍♂️

import React from "react";
import ReactDOM from "react-dom";
import createGlobalHook from "create-global-hook";

const [hook, Provider] = createGlobalHook((value: number) =>
  React.useState(value)
);

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

  // use your thing
};

const OtherApp = () => {
  const [state, setState] = hook();

  // same state here
};

ReactDOM.render(
  <Provider value={0}>
    <App />
    <OtherApp />
  </Provider>,
  document.getElementById("root")
);