0

I'm using react-router v4 for a React app.

In a form, I want to add query parameters on the onSubmit action

My code looks like this:

class FiltroCuentasCorrientes extends Component {
 ...//Other stuff.

 onSubmit(e) {
   e.preventDefault();
   const { someVariable, otherVariable } = this.state;
   //Here I should add the variables to the query param.
 }

 render() {
  <Form onSubmit={(e) => this.onSubmit(e)}>
    ...
  </Form>
  }
}

I don't know what the recommended approach is.

Should I have a variable in local state like shouldAddParam and render a <Redirect /> with my params?

Or use the router context and the navigateTo method?

How can I do that?

1 Answer 1

2

I would try to compose the action attribute of the form element during the render(), if that is possible. This way there's no need to prevent default.

However, if you want to "programatically redirect" using React router, then yes, you should use

this.context.router.push(path)

... as you can see in this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.