A configurable React component wrapper around CountUp.js.
Click here to view on CodeSandbox.
- Installation
- Usage
- API
- Props
className: stringdecimal: stringdecimals: numberdelay: ?numberduration: numberend: numberprefix: stringredraw: booleanpreserveValue: booleanseparator: stringstart: numberplugin: CountUpPluginstartOnMount: booleansuffix: stringuseEasing: booleanuseGrouping: booleanuseIndianSeparators: booleaneasingFn: (t: number, b: number, c: number, d: number) => numberformattingFn: (value: number) => stringenableScrollSpy: booleanscrollSpyDelay: numberscrollSpyOnce: booleanonEnd: ({ pauseResume, reset, start, update }) => voidonStart: ({ pauseResume, reset, update }) => voidonPauseResume: ({ reset, start, update }) => voidonReset: ({ pauseResume, start, update }) => voidonUpdate: ({ pauseResume, reset, start }) => void
- Render props
- Props
- Protips
- License
yarn add react-countupimportCountUpfrom'react-countup';<CountUpend={100}/>This will start a count up transition from 0 to 100 on render.
<CountUpstart={-875.039}end={160527.012}duration={2.75}separator=" "decimals={4}decimal=","prefix="EUR "suffix=" left"onEnd={()=>console.log('Ended! 👏')}onStart={()=>console.log('Started! 💨')}>{({ countUpRef, start })=>(<div><spanref={countUpRef}/><buttononClick={start}>Start</button></div>)}</CountUp>The transition won't start on initial render as it needs to be triggered manually here.
Tip: If you need to start the render prop component immediately, you can set delay={0}.
<CountUpstart={0}end={100}>{({ countUpRef, start })=>(<div><spanref={countUpRef}/><buttononClick={start}>Start</button></div>)}</CountUp>Render start value but start transition on first render:
<CountUpstart={0}end={100}delay={0}>{({ countUpRef })=>(<div><spanref={countUpRef}/></div>)}</CountUp>Note that delay={0} will automatically start the count up.
<CountUpdelay={2}end={100}/>import{useCountUp}from'react-countup';constSimpleHook=()=>{useCountUp({ref: 'counter',end: 1234567});return<spanid="counter"/>;};import{useCountUp}from'react-countup';constCompleteHook=()=>{constcountUpRef=React.useRef(null);const{ start, pauseResume, reset, update }=useCountUp({ref: countUpRef,start: 0,end: 1234567,delay: 1000,duration: 5,onReset: ()=>console.log('Resetted!'),onUpdate: ()=>console.log('Updated!'),onPauseResume: ()=>console.log('Paused or resumed!'),onStart: ({ pauseResume })=>console.log(pauseResume),onEnd: ({ pauseResume })=>console.log(pauseResume),});return(<div><divref={countUpRef}/><buttononClick={start}>Start</button><buttononClick={reset}>Reset</button><buttononClick={pauseResume}>Pause/Resume</button><buttononClick={()=>update(2000)}>Update to 2000</button></div>);};CSS class name of the span element.
Note: This won't be applied when using CountUp with render props.
Specifies decimal character.
Default: .
Amount of decimals to display.
Default: 0
Delay in seconds before starting the transition.
Default: null
Note:
delay={0}will automatically start the count up.
Duration in seconds.
Default: 2
Target value.
Static text before the transitioning value.
Forces count up transition on every component update.
Default: false
Save previously ended number to start every new animation from it.
Default: false
Specifies character of thousands separator.
Initial value.
Default: 0
Define plugin for alternate animations
Use for start counter on mount for hook usage.
Default: true
Static text after the transitioning value.
Enables easing. Set to false for a linear transition.
Default: true
Enables grouping with separator.
Default: true
Enables grouping using indian separation, f.e. 1,00,000 vs 100,000
Default: false
Easing function. Click here for more details.
Default: easeInExpo
Function to customize the formatting of the number.
To prevent component from unnecessary updates this function should be memoized with useCallback
Enables start animation when target is in view.
Delay (ms) after target comes into view
Run scroll spy only once
Callback function on transition end.
Callback function on transition start.
Callback function on pause or resume.
Callback function on reset.
Callback function on update.
Ref to hook the countUp instance to
Pauses or resumes the transition
Resets to initial value
Starts or restarts the transition
Updates transition to the new end value (if given)
By default, the animation is triggered if any of the following props has changed:
durationendstart
If redraw is set to true your component will start the transition on every component update.
You need to check if your counter in viewport, react-visibility-sensor can be used for this purpose.
importReactfrom'react';importCountUpfrom'react-countup';importVisibilitySensorfrom'react-visibility-sensor';import'./styles.css';exportdefaultfunctionApp(){return(<divclassName="App"><divclassName="content"/><VisibilitySensorpartialVisibilityoffset={{bottom: 200}}>{({ isVisible })=>(<divstyle={{height: 100}}>{isVisible ? <CountUpend={1000}/> : null}</div>)}</VisibilitySensor></div>);}Note: For latest react-countup releases there are new options
enableScrollSpyandscrollSpyDelaywhich enable scroll spy, so that as user scrolls to the target element, it begins counting animation automatically once it has scrolled into view.
import'./styles.css';importCountUp,{useCountUp}from'react-countup';exportdefaultfunctionApp(){useCountUp({ref: 'counter',end: 1234567,enableScrollSpy: true,scrollSpyDelay: 1000,});return(<divclassName="App"><divclassName="content"/><CountUpend={100}enableScrollSpy/><br/><spanid="counter"/></div>);}You can use callback properties to control accessibility:
importReactfrom'react';importCountUp,{useCountUp}from'react-countup';exportdefaultfunctionApp(){useCountUp({ref: 'counter',end: 10,duration: 2});const[loading,setLoading]=React.useState(false);constonStart=()=>{setLoading(true);};constonEnd=()=>{setLoading(false);};constcontainerProps={'aria-busy': loading,};return(<><CountUpend={123457}duration="3"onStart={onStart}onEnd={onEnd}containerProps={containerProps}/><divid="counter"aria-busy={loading}/></>);}import{CountUp}from'countup.js';import{Odometer}from'odometer_countup';exportdefaultfunctionApp(){useCountUp({ref: 'counter',end: 1234567,enableScrollSpy: true,scrollSpyDelay: 1000,plugin: Odometer,});return(<divclassName="App"><spanid="counter"/></div>);}MIT
