UNPKG

@openshift-console/dynamic-plugin-sdk

Version:

Provides core APIs, types and utilities used by dynamic plugins at runtime.

24 lines (23 loc) 830 B
import * as React from 'react'; /** * @deprecated - This hook is not related to console functionality. * Hook that ensures a safe asynchronnous setting of the React state in case a given component could be unmounted. * (https://github.com/facebook/react/issues/14113) * @param initialState initial state value * @returns An array with a pair of state value and its set function. */ export const useSafetyFirst = (initialState) => { const mounted = React.useRef(true); React.useEffect(() => { return () => { mounted.current = false; }; }, []); const [value, setValue] = React.useState(initialState); const setValueSafe = React.useCallback((newValue) => { if (mounted.current) { setValue(newValue); } }, []); return [value, setValueSafe]; };