React Router v6版中useHistory由useNavigate替代了

2023-05-292390次阅读React

React Router v6 版中 useHistory由useNavigate替代了

import React from 'react'
import { useNavigate } from 'react-router-dom'

function App() {
  let navigate = useNavigate()
  let [error, setError] = React.useState(null)

  async function handleSubmit(event) {
    event.preventDefault()
    let result = await submitForm(event.target)
    if (result.error) {
      setError(result.error)
    } else {
      navigate('success')
    }
  }

  return <form onSubmit={handleSubmit}>// ...</form>
}

更多React Router v6版特性看这里

上一篇: windows10下Ubuntu 20.04安装(亲测可用)  下一篇: MutationObserver监听文本是否修改  

React Router v6版中useHistory由useNavigate替代了相关文章