react-universally icon indicating copy to clipboard operation
react-universally copied to clipboard

[v13] Browser Back/Forward button in development mode logs "Warning: forceUpdate..."

Open haddoq opened this issue 9 years ago • 11 comments

If I use the browser back and forward button in the next branch I get this message in the console:

Warning: forceUpdate(...): Can only update a mounted or mounting component. This usually means you called forceUpdate() on an unmounted component. This is a no-op. Please check the code for the History component.

If i remove the react-async-component code the warning dissapears.

haddoq avatar Jan 30 '17 22:01 haddoq

Thanks for the report @haddoq!

ctrlplusb avatar Feb 01 '17 11:02 ctrlplusb

I found this bug as well few days back.

What I did was to disable the state output of the code split component.

@@ server/middleware/reactApplication/ServerHTML.js @@
    onlyIf(
-     asyncComponents,
+     process.env.BUILD_FLAG_IS_DEV === false && asyncComponents,
      () => inlineScript(
        `window.${asyncComponents.STATE_IDENTIFIER}=${serialize(asyncComponents.state)};`,
      ),
    ),

Few notes:

  1. The development server will not rehydrate state of code-splitted components.
  2. This may or may not cause problems for you.
  3. It's okay for me as I'm using mobx to handle state. (I don't use React's default state engine / setState).
  4. To fix this, something big needs to happen in the underlying package react-tree-walker.

birkir avatar Feb 20 '17 17:02 birkir

@birkir this is amazingly insightful thank you! I have some ideas forming...

ctrlplusb avatar Feb 20 '17 17:02 ctrlplusb

Hey all; I did some digging and because an initial parse is done on the react application (using react-tree-walker) some listeners get attached based on the Route components. The Route component in React Router does have a componentWillUnmount lifecycle event that cleans up these listeners, but it is against the SSR APIs / behaviour to fire this event. Therefore the listeners aren't cleaned up and then try to respond to events from our actual application execution. As this logic is lifted from the Apollo Stack, anyone using Apollo Stack with the latest React Router will experience this same issue.

I have logged an issue on React Router. Let's see if they guys have any sage advice for us.

https://github.com/ReactTraining/react-router/issues/4566

ctrlplusb avatar Feb 21 '17 00:02 ctrlplusb

Hey all. I have been trying to create a minimal example for the issue report on react router but it's a tricky case to recreate.

I have since discovered that this issue only arises in development mode. A production build/start works fine. So there appears to be something about the development build that leads to this error occurring. The error itself seems innocuous - the only effect it has is outputting to the console.

I am going to keep this as a "known issue" for now and will add it to the FAQ. In the interim I suggest you just ignore the console warnings rather than disabling the state in development as suggested by @birkir above - this would break the checksums in SSR mode. I find it's better to have the development environment as closely represent the behaviour of your production environment too (bar the hot reloading).

Again, this warning only occurs when using the browsers back/forward buttons when in development mode, but it has no effect on the app other than printing a warning message to the console.

ctrlplusb avatar Feb 21 '17 11:02 ctrlplusb

Ok good to know.

Are you sure the warnings are not happening in production as React won't output any warnings messages into the console while not using the development build.

In my case, these messages are really annoying and I would suppress them if I could identify them from other forceUpdate warnings caused by other things.

Maybe I'm just picky wanting a clean console 😄

I'll investigate this further when the time comes! Thanks for the input.

birkir avatar Feb 22 '17 14:02 birkir

Hmmm, that is a very good point! I initially took it as an "error" being thrown by the code in question, not that React was handling an expected error case. Definitely needs some more debugging then. The message itself states that it will be a noop, I guess my only concern is where and how loose component instances are staying alive. Thanks for the insight!

ctrlplusb avatar Feb 22 '17 15:02 ctrlplusb

New release of react-router will sort this. 🎉

ctrlplusb avatar Mar 04 '17 10:03 ctrlplusb

Sorted by #381

Thanks @datoml!

ctrlplusb avatar Mar 04 '17 15:03 ctrlplusb

@ctrlplusb Investigating another bug, so asking if you know what change in react-router solved this issue?

birkir avatar Mar 16 '17 10:03 birkir

No worries. It was their pub/sub system they used to track route changes. There are some lengthy blog posts around about the move away from this.

ctrlplusb avatar Mar 16 '17 10:03 ctrlplusb