azuldev
azuldev

Reputation: 694

How do I use async await when a component leaves view

How do I use Async Await ?

When this.props.onChangeStep1() is called, the component where the view is rendered is replaced by another component.

If the component has been swapped would Async Await still work ?

s3.upload(params, function(err, data) {

this.props.onChangeStep1(); //<----- After s3.upload I want to call this first 

            this.setState(   //<----But I want this to run in the background 
                                  // even though the component is not in view
                  {
                myState: "data-from-s3Upload"
              });
          }.bind(this)
        );

Upvotes: 1

Views: 194

Answers (2)

azuldev
azuldev

Reputation: 694

this.props.onChangeStep1(e) // <-- Forward data and Process logic on App.js / Parent Component 

Upvotes: 0

idmadj
idmadj

Reputation: 2645

If a callback is set to be executed later and the component that initiated the asynchronous call is unmounted, the callback will be executed regardless. If the callback attempts to act on the unmounted component (changing its state for instance) it will be considered as a memory leak and React will notify you with an error message in the console.

Upvotes: 1

Related Questions