React-router: Proposal: Use onbeforeunload for Prompt

Created on 1 Feb 2019  路  1Comment  路  Source: ReactTraining/react-router

Version

4.3

When preventing navigation with Prompt a custom message has to be provided, and it does a great job preventing push style navigation.

<Prompt 
    when={this.shouldBlockNavigation()} 
    message="Changes that you made may not be saved." 
/>

It looks like a normal prompt:

screen shot 2019-02-01 at 10 16 35

However this doesn't notify the user when refreshing the page or simply typing in a different URL. To prevent this, I have to use a window.onbeforeunload callback:

componentDidUpdate() {
  //...
  window.onbeforeunload = this.shouldBlockNavigation() ? alwaysTrue : undefined
}

screen shot 2019-02-01 at 10 16 48

Shouldn't the Prompt fall back to the default dialog, in case no message is provided? Is it even possible? The 2 different behaviour can be quite confusing for users (without why it happens).

Most helpful comment

4372

>All comments

4372

Was this page helpful?
0 / 5 - 0 ratings