UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

16 lines 881 B
import { useCallback, useState, } from 'react'; import { useSyncedRef } from '../performance-optimization/useSyncedRef'; // Assuming this exists and works export function useFunctionalState(initialState) { return useFunctionalStateFn(initialState); } const useFunctionalStateFn = (initialState) => { // useState handles the initial state (value or function) correctly. // If initialState is undefined (from overload 1), state starts as undefined. const [state, setState] = useState(initialState); // Use a synced ref to always have access to the latest state // within the useCallback without needing 'state' in the dependency array. const stateRef = useSyncedRef(state); const getState = useCallback(() => stateRef.current, []); // No dependencies needed due to ref return [getState, setState]; }; //# sourceMappingURL=useFunctionalState.js.map