@react-hookz/web
Version:
React hooks done right, for browser and SSR.
14 lines (13 loc) • 530 B
JavaScript
import { useState } from 'react';
import { useRafCallback } from '../useRafCallback/index.js';
import { useUnmountEffect } from '../useUnmountEffect/index.js';
/**
* Like `React.useState`, but state is only updated within animation frame.
*/
export function useRafState(initialState) {
// eslint-disable-next-line react/hook-use-state
const [state, innerSetState] = useState(initialState);
const [setState, cancelRaf] = useRafCallback(innerSetState);
useUnmountEffect(cancelRaf);
return [state, setState];
}