2

It is a question about strategy you would take. Mu issue concerns showing modal in a right moment, ie. when user uses a form and wants to go out of the form (which is component of course) by clicking any other link visible on the page I would like to show him something like: 'you're about to leave a page, are you sure to do that?' and buttons yes/no below it. So my first approach was to fire modal on componentWillUnmount lifecycle, but is seems that I cannot call it there as it won't accept any logic of deciding to leave or stay on the page (it will quit anyway). Also I cannot set state there and basing on that show modal. Maybe one of you had the issue? Sorry for not copying the code here but it is way too long ;)

Thank you!

Murakami
  • 3,474
  • 7
  • 35
  • 89
  • This might help you: https://stackoverflow.com/questions/32841757/detecting-user-leaving-page/45869459#45869459... – Janick Fischer Apr 20 '18 at 11:00
  • Possible duplicate of [Detecting user leaving page](https://stackoverflow.com/questions/32841757/detecting-user-leaving-page) – Bhojendra Rauniyar Apr 20 '18 at 11:03
  • Hah, you're probably right. I haven't thougth about it in that way, using router. I will give it a try. Thank you @JanickFischer – Murakami Apr 20 '18 at 11:03

1 Answers1

0

componentWillUnmount will never go backwards, you'll need to use the componentDidMount hook to use the functionality.

You may call an event handler on which you may also redirect to after the user confirms.

See this example: Detecting user leaving page

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231