【问题标题】:how to pass a variable to a component from history.replace如何将变量从 history.replace 传递给组件
【发布时间】:2021-11-06 07:58:11
【问题描述】:

如何在功能组件中访问这个 match.chatroomId ? 我使用 history.replace 重定向到组件 onClick = {() => history.replace(`/chat/${match.chatroomId}`)}

【问题讨论】:

    标签: javascript reactjs react-redux react-hooks react-router


    【解决方案1】:

    使用 react hook useParams() 这是 url - http://localhost:8000/personal/1。

    所以在这种情况下,use const { id } = useParams()

    现在,如果您执行 console.log(id) ,它将为您提供网址的确切 ID。在这种情况下,它会给你 1。

    useParams() 从 url 中提取 id 并让我们使用它。

    谢谢。

    【讨论】:

    • 你从反应库中导入了那个钩子
    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2023-03-15
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多