Languages
[Edit]
EN

React - create simple animated progress bar

3 points
Created by:
Krzysiek
651

Hello! 👋 😊

Today I want to show you a simple animated progress bar that I recently made in React.

Final effect of this post:

Animated progress bar in React
Animated progress bar in React

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: 

// ONLINE-RUNNER:browser;

// import React from 'react';
// import ReactDOM from 'react-dom';

const containerStyle = {
  	border: '1px solid silver',
  	background: '#ededed'
};

const contentStyle = {
  	background: '#00cc00',
  	height: '24px',
  	textAlign: 'center',
  	lineHeight: '24px',
  	fontFamily: 'sans-serif',
  	transition: '0.3s'
};

const ProgressBar = ({progress}) => {
  	const state = `${progress}%`;
    return (
      <div style={containerStyle}>
        <div style={{...contentStyle, width: state}}>
          {progress > 5 ? state : ''}
        </div>
      </div>
    );
};

const App = () => {
  const [progress, setProgress] = React.useState(25);
  return (
    <div>
      <ProgressBar progress={progress} />
      <br />
      <div>
        <button onClick={() => setProgress(0)}>0%</button>
        <button onClick={() => setProgress(5)}>5%</button>
        <button onClick={() => setProgress(15)}>15%</button>
        <button onClick={() => setProgress(50)}>50%</button>
        <button onClick={() => setProgress(75)}>75%</button>
        <button onClick={() => setProgress(100)}>100%</button>
      </div>
    </div>
  );
};

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

 References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS - Blog posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join