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.

Updating component that makes resource request triggers update warning

See original GitHub issue

Do you want to request a feature or report a bug?

Bug, but more of a minor annoyance.

What is the current behavior?

tl;dr Updating a component that makes a resource request which trigger state updates causes a warning to be displayed.

(This description is based off of a modified version of the unstable_async/fixtures example.)

Given a component that makes a resource call, which triggers React updates.

const someResource = createResource(fetchUserData);

function UserDetails(props) {
  // another component is "plugged in" to this resource, so
  // calling read() will trigger setState() calls in that component
  const data = someResource.read(props.id);
  return (
    <div>{data}</div>
  )
}

When the resource requesting component is mounting, it is an indeterminate component. During updates, it is known to be a functional component. In both instances, the component is called to get a return value. The difference being that when it is known to be a functional component, the renderer’s phase is set to render before calling the function.

// mountIndeterminateComponent()
const value = UserDetails(...);

// updateFunctionalComponent()
setCurrentPhase('render');
const value = UserDetails(...);
setCurrentPhase(null);

Because the phase is set to render for the functional component, we get a warning that React Cannot update during an existing state transition... (from warnAboutInvalidUpdates()).

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

https://codesandbox.io/s/0484pzv330

This is the unstable_async/suspense demo, but has been modified to add a button to navigate the user details for acdlite from any other user page.

  1. Click on any user (besides Andrew Clark, the warning is only triggered when his data is not cached).
  2. Click on the Go to Andrew Clark button.

In the console, there will be a warning about updating during an existing state transition.

What is the expected behavior?

No warning

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Master build

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gaearoncommented, Aug 30, 2018

Never mind, I was wrong. I missed that Promise constructor callback fires synchronously.

In that case the behavior makes sense to me. It’s indeed a setState in render which isn’t encouraged. My example does it because it’s hacky.

0reactions
gaearoncommented, Aug 30, 2018

I don’t understand where the fetcher synchronously calls setState. I only see an async call (so technically it doesn’t happen “during” render). Which means it might be a React bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't perform a React state update on an unmounted ...
This warning is due to when we do some fetch request while that request is in the background (because some requests take some...
Read more >
React: Stop checking if your component is mounted - Medium
This article will provide solutions to the “Can't perform a React state update on an unmounted component” warning and why a call to...
Read more >
React.Component
UNSAFE_componentWillUpdate() is invoked just before rendering when new props or state are being received. Use this as an opportunity to perform preparation ...
Read more >
Record Triggers for Flows That Make Before-Save Updates
Creating or updating a record can trigger an autolaunched flow to make additional updates to that record before it's saved to the database....
Read more >
8 common React error messages and how to address them
Whether you write code that causes these errors isn't important — nobody ... Can't perform a React state update on an unmounted component...
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