【问题标题】:Trying to use Redirect in ReactJS [duplicate]尝试在 ReactJS 中使用重定向 [重复]
【发布时间】:2020-10-13 18:42:22
【问题描述】:

当单击按钮但不起作用时,我正在尝试重定向。我对 handleClick 事件进行了控制台日志,它确实登录了控制台。这是代码,我不确定我哪里出错了?提前致谢。

import React from 'react'
import { Redirect } from "react-router-dom";

const Home = () => {

    function handleClick() {
       return <Redirect to='/aboutus' />
    }

    return (
        <div>
            <button type="button" onClick={handleClick}>
                Go About Us
            </button>
        </div>
    )
}

export default Home

【问题讨论】:

  • Redirect 必须像渲染函数中的视图一样渲染,使用状态才能正常工作。
  • 您更愿意寻找链接吗?如&lt;Link to="/aboutus"&gt;Go About Us&lt;/Link&gt;?

标签: reactjs


【解决方案1】:

手柄点击返回一个组件,没有实际动作。请改用链接。

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2023-02-24
    • 2015-03-18
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多