1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/hooks/useGlobalState.ts

24 lines
671 B
TypeScript
Raw Normal View History

import React from 'react';
import { createGlobalState } from 'react-hooks-global-state';
type UseGlobalState<T> = () => [
value: T,
setValue: React.Dispatch<React.SetStateAction<T>>
];
// Create a hook that stores global state (shared across all hook instances).
export const createGlobalStateHook = <T>(
key: string,
initialValue: T
): UseGlobalState<T> => {
const container = createGlobalState<{ [key: string]: T }>({
[key]: initialValue,
});
const setGlobalState = (value: React.SetStateAction<T>) => {
container.setGlobalState(key, value);
};
return () => [container.useGlobalState(key)[0], setGlobalState];
};