1

Progress Bar:

state = {
  progress: 0,
}

  render() {

    return (
       <Progress value={this.state.progress} title="Saving the details"/>

    );

This is my progress bar, i want this progress bar to increase dynamically with the interval of time. Can anyone help me with that?

2 Answers 2

2

Here you go :

componentDidMount() {
    let counter = 1;
    const interval = setInterval(() => {
        counter++;
        this.setState({
            progress : counter
        })
        if(counter == 100) {
            clearInterval(interval);
        }
    }, 100);
}

You can run the below code snippet and review :

const { useState , useEffect } = React;

class App extends React.Component {
  
  state = {
    progress : 0
  }

  componentDidMount() {
    let counter = 1;
    const interval = setInterval(() => {
        counter++;
        this.setState({
            progress : counter
        })
        if(counter == 100) {
            clearInterval(interval);
        }
    }, 100);
  }

  render() {
    return (
      <div>
        Progress : { this.state.progress }
      </div>
    );
  }
  
}

ReactDOM.render(<App />, document.getElementById('react-root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react-root"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

it's right. just use this code:

setInterval(() => {
  this.setState((prevState) => ({progress: prevState.progress + 10}))
}, 5000)

you can write this every where you want. like componentDidMount or in a custom function.

1 Comment

it is throwing an error in progress, saying any Property 'progress' does not exist on type 'Readonly<{}>'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.