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.

Use async/await in getDerivedStateFromProps

See original GitHub issue

Hi I’m in need of aysnc/await in getDerivedStateFromProps but it’s not working as expected.

static getDerivedStateFromProps(props, state) {
	const response = [];
	console.log('break 1');
	[1, 2].map(async path => {
		console.log('break 2');
		const data = await FileGetData.get(path);
		response.push(data);
	});
	console.log('break 3');
	return { fileData: response };
}

Expected

break 1
break 2
break 2
break 3

Results:

break1
break3
break2
break2

Can’t we use async/await in getDerivedStateFromProps?

Thanks,

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

22reactions
gaearoncommented, Sep 3, 2018

Don’t use getDerivedStateFromProps for data fetching. It’s not meant for this.

You want to put this logic into componentDidUpdate.

4reactions
gaearoncommented, Sep 5, 2018

Client implementation is more or less ready. The cache provider still needs work but the core implementation is already in production at FB.

We’re just starting work on the SSR part. So it might come later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Calling a asynchronous function inside ...
So I tried to call a function update inside getDerivedStateFromProps but it seems like I can't use 'this' inside getDerivedStateFromProps.
Read more >
Update on Async Rendering – React Blog
The new static getDerivedStateFromProps lifecycle is invoked after a component is instantiated as well as before it is re-rendered. It can ...
Read more >
How to use async/await in React props | by Mudafar El Halabi
How to use async/await in React props. A new way for bi-directional communication between react components.
Read more >
Rendering and Updating Data using Component Lifecycle ...
1async getTodos() { 2 let data = await axios 3 .get("https://jsonplaceholder.typicode.com/todos?_limit=10") 4 .then(function(response) { 5 ...
Read more >
AsyncButton | EF React Native Asynchronous Button
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked. deprecated. 16.3, use componentDidMount or the ...
Read more >

github_iconTop Related Medium Post

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