Answers for "detect browser back button click react"

2

if back react

import { useHistory } from 'react-router-dom'


const [ locationKeys, setLocationKeys ] = useState([])
const history = useHistory()

useEffect(() => {
  return history.listen(location => {
    if (history.action === 'PUSH') {
      setLocationKeys([ location.key ])
    }

    if (history.action === 'POP') {
      if (locationKeys[1] === location.key) {
        setLocationKeys(([ _, ...keys ]) => keys)

        // Handle forward event

      } else {
        setLocationKeys((keys) => [ location.key, ...keys ])

        // Handle back event

      }
    }
  })
}, [ locationKeys, ])
Posted by: Guest on July-05-2020
0

detect browser back button click react

not working
Posted by: Guest on April-03-2021

Code answers related to "detect browser back button click react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language