3

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

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

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.

Duke Dougal
  • 24,359
  • 31
  • 91
  • 123
  • Possible duplicate of https://stackoverflow.com/questions/44108850/adding-query-parameters-on-form-submission-react-router-4/44108971#44108971 – Shubham Khatri May 23 '17 at 12:04

1 Answers1

6

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

e.g.

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)
Alex Faunt
  • 529
  • 4
  • 6