counter and chart package added

This commit was merged in pull request #12.
This commit is contained in:
victorAnumudu
2025-04-13 11:19:19 +01:00
parent 55e5b9606a
commit f98299d39b
8 changed files with 382 additions and 31 deletions
+35 -25
View File
@@ -1,29 +1,39 @@
import React, { useEffect, useState } from 'react';
// import React, { useEffect, useState } from 'react';
// const CustomCounter = ({ targetNumber, timeInSeconds }) => {
// const [count, setCount] = useState(0);
// useEffect(() => {
// if (targetNumber <= 0 || timeInSeconds <= 0) return; // Handle edge cases
// const interval = Math.floor(timeInSeconds * 1000 / targetNumber); // Time interval for each count in milliseconds
// const totalTime = timeInSeconds * 1000; // Total time for the entire count in milliseconds
// let currentCount = 0;
// const intervalId = setInterval(() => {
// currentCount++;
// setCount((prevCount) => prevCount + 1); // Update state using the previous state
// if (currentCount >= targetNumber) {
// clearInterval(intervalId); // Stop the counting when the target number is reached
// }
// }, interval);
// // Cleanup the interval on component unmount
// return () => clearInterval(intervalId);
// }, [targetNumber, timeInSeconds]);
// return <>{count}</>;
// };
// export default CustomCounter;
import React from 'react';
import CountUp from 'react-countup';
const CustomCounter = ({ targetNumber, timeInSeconds }) => {
const [count, setCount] = useState(0);
useEffect(() => {
if (targetNumber <= 0 || timeInSeconds <= 0) return; // Handle edge cases
const interval = Math.floor(timeInSeconds * 1000 / targetNumber); // Time interval for each count in milliseconds
const totalTime = timeInSeconds * 1000; // Total time for the entire count in milliseconds
let currentCount = 0;
const intervalId = setInterval(() => {
currentCount++;
setCount((prevCount) => prevCount + 1); // Update state using the previous state
if (currentCount >= targetNumber) {
clearInterval(intervalId); // Stop the counting when the target number is reached
}
}, interval);
// Cleanup the interval on component unmount
return () => clearInterval(intervalId);
}, [targetNumber, timeInSeconds]);
return <>{count}</>;
return <CountUp end={targetNumber} duration={timeInSeconds} />;
};
export default CustomCounter;
export default CustomCounter;