Skip to content

Commit

Permalink
Huge performance issue fixed, components rendering infinite even task…
Browse files Browse the repository at this point in the history
… is finished
  • Loading branch information
pradeep-jais committed Dec 31, 2023
1 parent d30e110 commit 620fee4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
19 changes: 3 additions & 16 deletions src/projects/04-progressBar/Bars.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { useEffect, useState } from 'react';

const Bars = ({ count = 0, onComplete = () => {} }) => {
const [percentage, setPercentage] = useState(0);

if (count >= 100) {
if (count === 100) {
onComplete();
}

useEffect(() => {
if (count >= 0 && count <= 100) {
setPercentage(count);
}
// setPercentage(Math.min(100, Math.max(count, 0)));
}, [count]);

return (
<div className="bars">
<span style={{ color: percentage > 49 ? '#fff' : '#000' }}>
{percentage}%
</span>
<div style={{ right: `${100 - percentage}%` }}></div>
<span style={{ color: count > 49 ? '#fff' : '#000' }}>{count}%</span>
<div style={{ right: `${100 - count}%` }}></div>
</div>
);
};
Expand Down
29 changes: 20 additions & 9 deletions src/projects/04-progressBar/ProgressBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ const ProgressBar = () => {
const [count, setCount] = useState(0);
const [success, setSuccess] = useState(false);

const [stopCount, setStopCount] = useState(false);

useEffect(() => {
if (count === 100) {
setStopCount(true);
}
}, [count]);

useEffect(() => {
const id = setInterval(() => {
setCount((val) => {
return val + 1;
});
}, 100);
let id;
if (count < 100) {
id = setInterval(() => {
setCount((val) => {
// console.log(val, 'interval');
return val + 1;
});
}, 100);
}
return () => {
if (count > 100) {
clearInterval(id);
}
// console.log('cleanup, interval cleared');
clearInterval(id);
};
}, []);
}, [stopCount]);

return (
<section className="section">
Expand Down

0 comments on commit 620fee4

Please sign in to comment.