Giter VIP home page Giter VIP logo

Comments (4)

dtn1999 avatar dtn1999 commented on July 18, 2024 3

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.

genikineg avatar genikineg commented on July 18, 2024

Thanks!

from react-countup.

hamzapaskingakhtar1999 avatar hamzapaskingakhtar1999 commented on July 18, 2024

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.

hamzapaskingakhtar1999 avatar hamzapaskingakhtar1999 commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.