【问题标题】:In react-router V4 how can I programmatically set the query string params?在 react-router V4 中,如何以编程方式设置查询字符串参数?
【发布时间】:2017-10-23 08:36:17
【问题描述】:

与此处相同的问题,但针对 react-router 版本 4。

How do you programmatically update query params in react-router?

这可能会被关闭,因为我没有任何代码要显示,但在 github 他们说“在 Stack Overflow 上询问”,据我所知,它不在文档中。

【问题讨论】:

标签: reactjs react-router


【解决方案1】:

您可以使用withRouter higher order component 直接访问历史记录。

例如

import React, { Component } from 'react
import { withRouter } from 'react-router'

class MyComp extends Component {
  doStuff = () => {
    this.props.history.push({
      pathname: '/pathname',
      search: '?stuff=done'
    })
  }

  render () {
    <button onClick={this.doStuff}>Do stuff</button>
  }
}

export default withRouter(MyComp)

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2017-10-23
    • 2017-03-02
    • 2017-09-22
    • 2017-08-30
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多