META-INF.resources.js.utils.useSafeReducer.ts Maven / Gradle / Ivy
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
import {useIsMounted} from '@liferay/frontend-js-react-web';
import {Reducer, ReducerState, useCallback, useReducer} from 'react';
const identity = (a: T) => a;
export function useSafeReducer, I>(
reducer: R,
initializerArg: I & ReducerState,
initializer: (arg: I & ReducerState) => ReducerState = identity
) {
const [state, dispatch] = useReducer(reducer, initializerArg, initializer);
const isMounted = useIsMounted();
const safeDispatch = useCallback(
(...args: Parameters) => {
if (isMounted()) {
dispatch(...args);
}
},
[dispatch, isMounted]
);
return [state, safeDispatch] as const;
}