Created
April 13, 2022 13:01
-
-
Save amcdnl/e6b1dbd4e7462093522ccc9660502372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { animate } from "framer-motion"; | |
import React, { useEffect, useRef } from "react"; | |
function Counter({ from, to }) { | |
const ref = useRef(); | |
from https://stackoverflow.com/questions/65013742/how-to-animate-number-with-framer-motion | |
useEffect(() => { | |
const controls = animate(from, to, { | |
duration: 1, | |
onUpdate(value) { | |
ref.current.textContent = value.toFixed(1); | |
} | |
}); | |
return () => controls.stop(); | |
}, [from, to]); | |
return <p ref={ref} />; | |
} | |
export default function App() { | |
return <Counter from={0} to={65.4} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment