question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request Feature: Add API afterSetState

See original GitHub issue

I want to request a feature.

Could we add an API named afterSetState on class Component.

afterSetState(state){
  return new Promise(resolve=>this.setState(state,resolve));
}

Then ,sometimes we can do something after update state. I do this in my codes . if we use async/await,it’s a better API.

etc:


componentWillMount(){
   this.init();
}

async setPageLoading(isLoading){
    return this.afterSetState({isLoading});
}

async update(){
   await this.setPageLoading(true);
   let data = await fetch('data url'); 
   await this.afterSetState(data);
   //some business...
   await this.setPageLoading(false);
}

async init(){
   await this.update();
   this.adjustSomthingDomRelated();
}

i wish it can be added into react.js in future version.

Promise is not the question.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bvaughncommented, Mar 30, 2018

No problem. 😄

I’m going to close this issue. Personally I’m not convinced this is a necessary addition or that it solves any problems not already solved by componentDidUpdate or the setState callback- but if anyone would like to make a case for that- please open a PR in the RFCs repo and let’s see what people think! 😁

1reaction
raunofreibergcommented, Mar 29, 2018

setState accepts a callback function so you could just do

this.setState(
    {
        hello: "Hello World"
    },
    () => {
        // your callback code here
    }
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I execute a function after setState is finished updating?
You can execute a function after setState is finishing using the second param callback like: this.setState({ someState: obj }, () => { this....
Read more >
How to Use the setState Callback in React - Upmostly
To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use...
Read more >
React — execute a function with latest state after setState
I have this scenario that I update the state with calling setState, then I call the api to get the list back. Instead...
Read more >
React Execute Code Immediately After Set State Update and ...
We want to run a piece of code not just after a state update call but ... import { useState } from 'react';...
Read more >
How to Build a React Application with Load More Functionality ...
In this article, we will build a React application using class components. Then we'll convert it to functional components using React Hooks ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found