Change View of a page after sending data

mary_jane

Initially I am rendering Upload file text with a button.After clicking this it is calling _handleSubmit which sends file to an API.
After this sending is done I want to change Upload file to Upload done.For that I have created a variable isSend but not able to use it.

 _handleSubmit(e) {
    e.preventDefault();
    const fl = new FormData();
    fl.append("name", this.state.file);
    const req = request
      .post("/upload")
      .send(fl);
    req.end(function (err, response) {
      if (response=== "OK") {
        this.setState({//not setting showing error
          isSend: true
        });
      }
    });

  }



 render() {
    const isSend = this.state.isSend; //false initially
    return (
      <div>
          <h3>Upload file</h3> //Show done upload after done

            <button 
                    type="submit"
                    onClick={(e) => this._handleSubmit(e)}>Upload File
            </button>
        </div>

    )
  }
Mayank Shukla

You forgot to bind the context, Use this:

req.end((err, response) => {
     if (response=== "OK") {
        this.setState({  //it will work
            isSend: true
        });
     }
 });

or use .bind(this) with callback method, like this:

req.end( function(err, response) {
    if (response=== "OK") {
      this.setState({  //it will work
          isSend: true
      });
    }
}.bind(this));

And check the value of isSend inside render method to change the text, like this:

render() {
    const isSend = this.state.isSend; //false initially
    return (
        <div>
            <h3> {isSend ? 'Upload Done' : 'Upload file' } </h3> 
            <button 
                type="submit"
                onClick={(e) => this._handleSubmit(e)}>Upload File
            </button>
        </div>

    )
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change ng-view after get data in Angular.JS

From Dev

Html is not changed in my backbone view after change data

From Dev

Sending data into a nested Controller/View in EmberJS

From Dev

Getting JsonMappingException while sending data to view

From Dev

After sending for array is displayed in the page

From Dev

Android View Empty After Sending Email

From Dev

Sending Data to database without refreshing page

From Dev

Change the Value of a Label in a page before sending to mail

From Dev

Sending data from function to view template?

From Dev

Sending form data to page nested in div

From Dev

Sending and Receive data from a web page - Selenium

From Dev

Sending data from Controller to View in Laravel 7

From Dev

Sending JSON data to next jsp page

From Dev

Sending Data to database without refreshing page

From Dev

Sending data to a view extended by other views in Laravel

From Dev

Sending Data From One Page To The Other Page

From Dev

How can I remove the double quotes after sending data to view with ViewBag?

From Dev

How to change data before sending to ListView on Android

From Dev

Change the Value of a Label in a page before sending to mail

From Dev

Sending form data to page nested in div

From Dev

Sending data to my master page

From Dev

Automatic INotifyPropertyChanged after change in View

From Dev

Change Record View in page

From Dev

Sending data after password validation

From Dev

change the image after refresh the page

From Dev

Dynamically adding data to page with aurelia after the view is rendered

From Dev

Hiding page loader only after completion of data load in the page view

From Dev

String data not sending with $.ajax call to php page

From Dev

Smtp client hangs after sending data

Related Related

  1. 1

    Change ng-view after get data in Angular.JS

  2. 2

    Html is not changed in my backbone view after change data

  3. 3

    Sending data into a nested Controller/View in EmberJS

  4. 4

    Getting JsonMappingException while sending data to view

  5. 5

    After sending for array is displayed in the page

  6. 6

    Android View Empty After Sending Email

  7. 7

    Sending Data to database without refreshing page

  8. 8

    Change the Value of a Label in a page before sending to mail

  9. 9

    Sending data from function to view template?

  10. 10

    Sending form data to page nested in div

  11. 11

    Sending and Receive data from a web page - Selenium

  12. 12

    Sending data from Controller to View in Laravel 7

  13. 13

    Sending JSON data to next jsp page

  14. 14

    Sending Data to database without refreshing page

  15. 15

    Sending data to a view extended by other views in Laravel

  16. 16

    Sending Data From One Page To The Other Page

  17. 17

    How can I remove the double quotes after sending data to view with ViewBag?

  18. 18

    How to change data before sending to ListView on Android

  19. 19

    Change the Value of a Label in a page before sending to mail

  20. 20

    Sending form data to page nested in div

  21. 21

    Sending data to my master page

  22. 22

    Automatic INotifyPropertyChanged after change in View

  23. 23

    Change Record View in page

  24. 24

    Sending data after password validation

  25. 25

    change the image after refresh the page

  26. 26

    Dynamically adding data to page with aurelia after the view is rendered

  27. 27

    Hiding page loader only after completion of data load in the page view

  28. 28

    String data not sending with $.ajax call to php page

  29. 29

    Smtp client hangs after sending data

HotTag

Archive