Comments (4)
I came across the same issue when working on one project.
Basically, you need a reliable way to know if the component is present in the viewport and render.
Thanks to the react-intersection-observer you can know precisely when the component is in the review port and the code below gives you the result you want.
import React from "react";
import ReactCounUp, { CountUpProps } from "react-countup";
import { useInView } from "react-intersection-observer";
// wrapper component
const CountUp: React.FC<CountUpProps> = React.memo(({ ...props }) => {
const [startValue, setStartValue] = React.useState<number>();
const { ref, inView, entry } = useInView({
/* Optional options */
threshold: 0,
});
// just so that the redraw property is set to true later, May there be is a better way to do that
const { redraw, duration, ...rest } = props;
// only re-ready when the focus change [inView]
React.useEffect(() => {
if (inView) {
setStartValue(0);
// start the count up
} else {
setStartValue(undefined);
}
}, [inView]);
return (
<div ref={ref}>
<ReactCounUp start={startValue} redraw {...rest} />
</div>
);
});
from react-countup.
Thanks!
from react-countup.
It seems to be working fine when I run in sandbox. It re renders when I visit the section again. I tried the same but it has bugs. Works fine on Sandbox to me
from react-countup.
Found a solution to this from someone on StackOverflow.
import VisibilitySensor from 'react-visibility-sensor'; /* Install this dependency */
<CountUp end={100} redraw={true}> {({ countUpRef, start }) => ( <VisibilitySensor onChange={start} delayedCall> <span ref={countUpRef} /> </VisibilitySensor> )} </CountUp>
from react-countup.
Related Issues (20)
- scrollSpyOnce does nothing HOT 1
- Missing/Incomplete Types HOT 1
- missing types HOT 3
- useEasing defaults to undefined instead of true HOT 1
- hook 写法有 bug HOT 2
- TS: 类型缺失
- Help with onEnd
- No start value on SSR HOT 1
- Cannot read properties of null (reading 'tagName') HOT 19
- When suffix and/or prefix are undefined, they get converted to the string 'undefined' HOT 1
- 6.4.2 broken counting speed HOT 1
- SyntaxError: Unexpected token 'export' HOT 24
- Add how to use react-countup with NextJS 13 app directory HOT 3
- SyntaxError: Unexpected token 'export' HOT 1
- AWS Amplify - TypeError: Cannot read properties of null (reading 'tagName')
- When the component initializes and loads, the HTML code will flash on the page for a few seconds
- enableScrollSpy disables onEnd and onStart callback func HOT 3
- react-countup fails to work in next.js produnction environment HOT 4
- [Bug] `startOnMount` does not exist HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-countup.