MCPcopy Index your code
hub / github.com/streamich/react-use / useRafState

Function useRafState

src/useRafState.ts:5–22  ·  view source on GitHub ↗
(initialState: S | (() => S))

Source from the content-addressed store, hash-verified

3import useUnmount from './useUnmount';
4
5const useRafState = <S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] => {
6 const frame = useRef(0);
7 const [state, setState] = useState(initialState);
8
9 const setRafState = useCallback((value: S | ((prevState: S) => S)) => {
10 cancelAnimationFrame(frame.current);
11
12 frame.current = requestAnimationFrame(() => {
13 setState(value);
14 });
15 }, []);
16
17 useUnmount(() => {
18 cancelAnimationFrame(frame.current);
19 });
20
21 return [state, setRafState];
22};
23
24export default useRafState;

Callers 6

DemoFunction · 0.90
useScrollFunction · 0.85
useWindowSizeFunction · 0.85
useWindowScrollFunction · 0.85
useMouseFunction · 0.85

Calls 2

setStateFunction · 0.85
useUnmountFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…