【问题标题】:React and React-Router: Routing through function call with params passed into the route instead of clicking <Link>React 和 React-Router:通过函数调用进行路由,参数传递到路由中,而不是单击 <Link>
【发布时间】:2018-04-22 15:36:27
【问题描述】:

如何通过 react-router 中的函数调用传递参数进行路由?

例如,我有这个链接,当点击路由到带有传入参数的不同网址时:

<Link key={category.name} to={`/category/${category.name}`} params={{category: category.name}} >{category.name}</Link>

假设我想通过一个函数调用来模拟点击上述链接时会发生什么,该怎么做呢?

clickHandler(){
//What function should be called to do the routing through a function call?

}

【问题讨论】:

标签: reactjs react-router


【解决方案1】:

使用history 对象。

如果渲染Link 的组件是由路由器渲染的,那么您在this.props 中有一个可以使用的history 属性。

clickHandler(){
    const {
        history
        category // ??
    } = this.props
    history.push(`/category/${category.name}`);
}

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 2017-08-01
    • 2016-10-14
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多