EN
React - create simple animated progress bar
3 points
Hello! ๐ ๐
Today I want to show you a simple animated progress bar that I recently made in React.
Final effect of this post:

Below I present you my solution for a simple progress bar with some styling ๐๐จ.
In this solution I use:
useState
hook to store the progress bar's state,- content
width
measured in percents according to the container - that trick lets us display progress from 0% to 100% in an easy way, - some example buttons that trigger
setProgress()
method to demonstrate how progress bar works - animation between switching has a nice effect.
Practical example:
xxxxxxxxxx
1
// import React from 'react';
2
// import ReactDOM from 'react-dom';
3
โ
4
const containerStyle = {
5
border: '1px solid silver',
6
background: '#ededed'
7
};
8
โ
9
const contentStyle = {
10
background: '#00cc00',
11
height: '24px',
12
textAlign: 'center',
13
lineHeight: '24px',
14
fontFamily: 'sans-serif',
15
transition: '0.3s'
16
};
17
โ
18
const ProgressBar = ({progress}) => {
19
const state = `${progress}%`;
20
return (
21
<div style={containerStyle}>
22
<div style={{contentStyle, width: state}}>
23
{progress > 5 ? state : ''}
24
</div>
25
</div>
26
);
27
};
28
โ
29
const App = () => {
30
const [progress, setProgress] = React.useState(25);
31
return (
32
<div>
33
<ProgressBar progress={progress} />
34
<br />
35
<div>
36
<button onClick={() => setProgress(0)}>0%</button>
37
<button onClick={() => setProgress(5)}>5%</button>
38
<button onClick={() => setProgress(15)}>15%</button>
39
<button onClick={() => setProgress(50)}>50%</button>
40
<button onClick={() => setProgress(75)}>75%</button>
41
<button onClick={() => setProgress(100)}>100%</button>
42
</div>
43
</div>
44
);
45
};
46
โ
47
const root = document.querySelector('#root');
48
ReactDOM.render(<App />, root);