react-use/docs/createGlobalState.md
MrHuangJser 08d13933e3
Update docs/createGlobalState.md
fix spell issue

Co-Authored-By: Vadim Dalecky <streamich@users.noreply.github.com>
2020-01-14 08:57:59 +08:00

586 B

useGlobalState

A React hook which creates a globally shared state.

Usage

const useGlobalValue = createGlobalState<number>(0);

const CompA: FC = () => {
  const [value, setValue] = useGlobalValue();

  return <button onClick={() => setValue(value + 1)}>+</button>;
};

const CompB: FC = () => {
  const [value, setValue] = useGlobalValue();

  return <button onClick={() => setValue(value - 1)}>-</button>;
};

const Demo: FC = () => {
  const [value] = useGlobalValue();
  return (
    <div>
      <p>{value}</p>
      <CompA />
      <CompB />
    </div>
  );
};