reactjs 我如何更改页面而不React挂钩?

k2fxgqgv  于 2022-12-12  发布在  React
关注(0)|答案(1)|浏览(146)

怎么还能驾轻就熟?

import ButtonLogReg from "../components/ButtonLogReg"
import { Link } from 'react-router-dom'
import { useContext } from 'react'
import Context from '../components/Context'
import { useNavigate } from 'react-router-dom'

function Launcher() {
    log.info('Home -> render')

    const { showAlert } = useContext(Context)

    const handler = event => {

      event.preventDefault()
      try(
         asyncFunction()
         )catch(error)
    }

    return <main>
        <nav>
            <h2 >Go?</h2>
            <Link to="/Mypath>"<ButtonLogReg onClick={handler}>READY</ButtonLogReg></Link>
            <Link to="/"><h2>Back</h2></Link>
        </nav>
    </main>
}

export default Launcher

大家好,我试着在/Mypath导航,但是我不能用它做这个,我怎么能做这个呢?

ia2d9nvy

ia2d9nvy1#

希望您已经在App.js中设置了路由,那么这可能会对您有所帮助:

import ButtonLogReg from "../components/ButtonLogReg"
import { useContext } from 'react'
import Context from '../components/Context'
import { useNavigate } from 'react-router-dom'

function Launcher() {
    log.info('Home -> render')

    const { showAlert } = useContext(Context)

    const handler = event => {

        event.preventDefault()
        try(
            asyncFunction()
        )catch (error)
        }

    const navigate = useNavigate();

    return <main>
        <nav>
            <h2 >Go?</h2>
            <ButtonLogReg onClick={() => navigate("/Mypath")} >READY</ButtonLogReg>
            <h2 onClick={() => navigate("/")} >Back</h2>
        </nav>
    </main>
}

export default Launcher

相关问题