Create smooth animation in React components with ~60FPS.
npm install react-frame-rate!Demo
npm install react-frame-rate --save
oryarn add react-frame-rate
typescript
import * as React from "react";import { useFrameRateManager } from "react-frame-rate";
export function Counter() {
const [counter, setCounter] = React.useState(0);
const {
updateCallback,
updateFrameRate,
updateAnimation
} = useFrameRateManager();
React.useEffect(() => {
updateCallback(() => setCounter((value) => value + 1));
}, [updateCallback, setCounter]);
React.useEffect(() => {
updateFrameRate(30);
}, [updateFrameRate]);
React.useEffect(() => {
updateAnimation(true);
return () => {
updateAnimation(false);
};
}, [updateAnimation]);
return
{counter};
}
`
#### Props:
- updateCallback - set callback which is called on each frame.
- updateFrameRate - set desired frame rate value, optimal values 60/30/20/15/10/6/5/3/1.
- updateAnimation- set start/stop animation with boolean flag.$3
`typescript
import * as React from "react";import withReactFrameRate, { BaseUpdateProps } from "react-frame-rate";
type Props = Readonly<{
counter: number;
}> &
BaseUpdateProps;
export function Counter() {
const [isAnimating, setIsAnimation] = React.useState(true);
const updateState = React.useCallback<(state: Props) => Props>(
(state: Props) => {
const newCounter = state.counter + 1;
if (newCounter >= 100) {
setIsAnimation(false);
}
return { ...state, counter: newCounter };
},
[setIsAnimation]
);
const options = React.useMemo(
() => ({
updateState,
frameRate: 30
}),
[updateState]
);
const WithAnimation = React.useMemo(
() =>
withReactFrameRate(options)((props: Props) => (
<>{props.counter}>
)),
[options]
);
return ;
}
`#### Options:
-
updateState - refresh state on each frame.
- frameRate - current frame rate for updateState.
For efficient animation use frameRate - 60/30/20/15/10/6/5/3/1.$3
$3
Contributing
Contributing are Welcome 🎉
Please check out the Contributing guide.
$3
$3
requestAnimatioFrame react smooth animation`