0.1.1 • Published 4 years ago

create-global-hook v0.1.1

Weekly downloads
2
License
MPL-2.0
Repository
github
Last release
4 years ago

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")
);