Request Feature: Add API afterSetState
See original GitHub issueI 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:
- Created 5 years ago
- Comments:15 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 thesetState
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! 😁setState
accepts a callback function so you could just do