@react-hookz/web
Version:
React hooks done right, for browser and SSR.
11 lines (10 loc) • 390 B
JavaScript
import { useRafCallback, useSafeState, useUnmountEffect } from '..';
/**
* Like `React.useState`, but state is only updated within animation frame.
*/
export function useRafState(initialState) {
const [state, innerSetState] = useSafeState(initialState);
const [setState, cancelRaf] = useRafCallback(innerSetState);
useUnmountEffect(cancelRaf);
return [state, setState];
}