React-router: Prompt in React 16 "Warning: A history supports only one prompt at a time"

Created on 9 Nov 2017  路  9Comments  路  Source: ReactTraining/react-router

Version

react-router-dom 4.2.2
react 16.0.0

Test Case

https://codesandbox.io/s/rrn9vww8pn

Cause

There is a breaking change in React 16:

When replacing <A /> with <B />, B.componentWillMount now always happens before A.componentWillUnmount. Previously, A.componentWillUnmount could fire first in some cases.

In the Test Case above each page (Home, About, Topics) has a Prompt component. Because of the asynchronous nature of the React 16 lifecycle, history warns about two prompts being mounted at the same time:

Warning: A history supports only one prompt at a time

At no point in time are there actually two Prompt components "on screen", but this warning does get triggered.

Issue https://github.com/ReactTraining/history/issues/536 is caused by the same breaking change.
Perhaps issue https://github.com/facebook/react/issues/11106 will provide an answer how to resolve this problem.

stale

Most helpful comment

i solved it finnaly
promptwrapper.js:
image

your page.js:
image

All 9 comments

I wonder if we can wrap this line in a setTimeout and call it a day?

@timdorr The problem is componentWillUnmount triggers _after_ componentWillMount. Your solution doesn't change that.

Using setTimeout in componentWillMount _might_ change the order, but I'm not sure you have any guarantee it will.

Yeah, sorry, I had that backwards. What about changing to cDM? Will that work? I'm not a user of <Prompt>, so I'm not well-versed here.

cDM should work since it will be called after the cWU of the unmounting component.

We have the same issue, do you have some workaround guys?

The suggested workaround to setting 'when' to true in cDM works. Prompt.js should follow this suggestion (https://github.com/facebook/react/issues/11106) and update its code to use cDM instead of cWM. This would get rid of the warning message.

i have the same issue too, it makes me headache . sos

i solved it finnaly
promptwrapper.js:
image

your page.js:
image

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

misterwilliam picture misterwilliam  路  3Comments

andrewpillar picture andrewpillar  路  3Comments

imWildCat picture imWildCat  路  3Comments

Waquo picture Waquo  路  3Comments

ArthurRougier picture ArthurRougier  路  3Comments