在 react-router V4 中,如何以编程方式设置查询字符串参数? [英] In react-router V4 how can I programmatically set the query string params?

查看:23
本文介绍了在 react-router V4 中,如何以编程方式设置查询字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Same question as here, but for react-router version 4.

如何在 React 中以编程方式更新查询参数-路由器?

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

This will probably be closed because I don't have any code to show, but at github they say "ask on Stack Overflow", and it's not in the documentation as far as I can see.

推荐答案

您可以使用 withRouter高阶组件直接访问历史.

You can use the withRouter higher order component to access the history directly.

例如

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)

这篇关于在 react-router V4 中,如何以编程方式设置查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆