useRafState
Update the state in [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) callback, generally used for performance optimization.
Overview
Update the state in requestAnimationFrame callback, generally used for performance optimization.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useRafState.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useRafState.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useRafState.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useRafState.jsonUpdate the state in requestAnimationFrame callback, generally used for performance optimization.
Examples
Default usage
API
Same as React.useState.
useRafInterval
A hook implements with requestAnimationFrame for better performance. The API is consistent with useInterval, the advantage is that the execution of the timer can be stopped when the page is not rendering, such as page hiding or minimization.
useRafTimeout
A hook implements with requestAnimationFrame for better performance. The API is consistent with useTimeout. the advantage is that will not trigger function when the page is not rendering, such as page hiding or minimization.