Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fdev opened this issue Nov 9, 2017 · 9 comments
Closed
Labels

Comments

@fdev
Copy link

fdev commented Nov 9, 2017

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 remix-run/history#536 is caused by the same breaking change.
Perhaps issue facebook/react#11106 will provide an answer how to resolve this problem.

@timdorr
Copy link
Member

timdorr commented Nov 9, 2017

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

@fdev
Copy link
Author

fdev commented Nov 9, 2017

@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.

@timdorr
Copy link
Member

timdorr commented Nov 9, 2017

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.

@pshrmn
Copy link
Contributor

pshrmn commented Nov 9, 2017

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

@bora89
Copy link

bora89 commented Feb 27, 2018

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

@kbaird5
Copy link

kbaird5 commented Mar 26, 2018

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

@xuiwife
Copy link

xuiwife commented Jun 29, 2018

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

@xuiwife
Copy link

xuiwife commented Jul 2, 2018

i solved it finnaly
promptwrapper.js:
image

your page.js:
image

@stale
Copy link

stale bot commented Sep 10, 2019

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.

@stale stale bot added the stale label Sep 10, 2019
@stale stale bot closed this as completed Sep 17, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants