1

Is there any simple and convenient way to navigate to same route with different params?

for ex. I'm right now on /profile/idan and I wish to change the param of idan to something else, instantly without dealing with the static part of the route.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Idan Gozlan
  • 3,173
  • 3
  • 30
  • 47

1 Answers1

-2

If your question is about v3 of the API, the react-router library introduces a router context. If you declare that in your contextTypes, you'll have access to a push and replace method. Example:

import React, { Component, PropTypes } from 'react';
import { PropTypes as RouterPropTypes } from 'react-router';

class MyComponent extends Component {
  // ...
  handleSomeEvent() {
    this.context.router.push('/some/url');
  }
}

MyComponent.contextTypes = {
  router: RouterPropTypes.routerShape
};

export default MyComponent;
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • It's v3, and actually `push` is what I don't like to use because I can't switch the params, I have to specify the whole route, which my main question is how to avoid it. Thanks anyway. – Idan Gozlan Mar 31 '17 at 01:06
  • I see. They explicitly removed support for things like that. You may want to look at https://github.com/ReactTraining/react-router/issues/1840 for context. Essentially the solution is "build your own," and use the low-level push mechanism. There is no functionality for route URL building. – Jacob Mar 31 '17 at 01:43