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
olav 94ecaa80a8 refactor: improve feature toggle search state (#741)
* refactor: rename createPersistentGlobalStateHook helper

* refactor: move features filter state out of localStorage

* refactor: show search state in page title

* refactor: remove unused import

* refactor: add a state chip to SearchField

* refactor: improve var names
2022-02-23 15:08:44 +01:00

24 lines
671 B
TypeScript

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];
};