1

I have developed a responsive website using React js.and i want to show confirmation popup when user press on the Back button of android/ios mobile How to detect if the back button is pressed in a mobile phone using react js or node js

Ramusesan
  • 854
  • 2
  • 11
  • 32

1 Answers1

0

Here is how I'm doing it, suppose I have three child routes like home, data & user under dashboard

 handleBackButton() {
  this._isMounted = true;
  window.onpopstate = ()=> {
   if(this._isMounted) {
    const { hash } = location;
    if(hash.indexOf('home')>-1 && this.state.value!==0)
      //Your action
    if(hash.indexOf('users')>-1 && this.state.value!==1)
     //Your action
    if(hash.indexOf('data')>-1 && this.state.value!==2)
      //Your action
  }
}

}

Prabhu Tiwari
  • 109
  • 1
  • 7
  • Is there no way to detect mobile back button using like event.keyCode – Ramusesan Jun 01 '18 at 12:45
  • 1
    Above code is working on mobile also, actually it is not sending the hardware button press event rather it is catching your route change event. – Prabhu Tiwari Jun 01 '18 at 12:50
  • Just saying, if this is a repost of somebody else's answer you should provide credit where it is due...https://stackoverflow.com/a/39363278/8382028 – ViaTech Jun 01 '18 at 12:52
  • May be someone has posted the solution, but I have used this code in my project few months back thats why I have answered you, if I have seen this post before answering you then definitely I have given you url rather than answer. – Prabhu Tiwari Jun 01 '18 at 12:56