Exploring the concept of combining multiple progress bars into one entity... Discover how to create a React progress bar below:
Main Component:
class ProgressBarExample extends React.Component {
state = { percentage: 0 };
nextStep = () => {
if (this.state.percentage === 100) return;
this.setState({ percentage: this.state.percentage + 20 });
};
render() {
return (
<div>
<h2> A React Progress Bar </h2>
<ProgressBar percentage={this.state.percentage} />
<div style={{ marginTop: '20px' }}>
<button onClick={this.nextStep}>Next Step</button>
</div>
<div
style={{ marginTop: '10px', color: 'blue', marginBottom: '15px' }}
onClick={() => this.setState({ percentage: 0 })}
>
Reset
</div>
</div>
);
}
}
Check out the customized progress bar component:
const ProgressBar = props => {
return (
<div className="progress-bar">
<Filler percentage={props.percentage} />
</div>
);
};
And here is the filler element:
const Filler = props => {
return <div className="filler" style={{ width: `${props.percentage}%` }} />;
};
Experience it live with this demo link: https://codesandbox.io/s/wk628wy5yk