“Tombol Kembali Chrome Klik Event React” Kode Jawaban

Jika kembali bereaksi

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, ])
Wide-eyed Weevil

Tombol Kembali Chrome Klik Event React

const {history} = useRouter();
  useEffect(() => {
    return () => {
      // && history.location.pathname === "any specific path")
      if (history.action === "POP") {
        history.replace(history.location.pathname, /* the new state */);
      }
    };
  }, [history])
Tense Thrush

Jawaban yang mirip dengan “Tombol Kembali Chrome Klik Event React”

Pertanyaan yang mirip dengan “Tombol Kembali Chrome Klik Event React”

Lebih banyak jawaban terkait untuk “Tombol Kembali Chrome Klik Event React” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya