9

I've been learning React along with Redux, and have noticed two ways to change the URL programmatically (not using <Link>) when using React-Router.

One way is to push directly to browserHistory.

import React from 'react';
import { browserHistory } from 'react-router';

class Name extends React.Component {
    gotoPage() {
        browserHistory.push('/page');
    }
    render() {
       return <div onClick={this.gotoPage.bind(this)}>Hello</div>
    }
}

The other is to push to this.context.router.

import React, { PropTypes } from 'react';
import { browserHistory } from 'react-router';

class Name extends React.Component {
    static contextTypes = {
        router: PropTypes.object
    };
    gotoPage() {
        this.context.router.push('/page');
    }
    render() {
       return <div onClick={this.gotoPage.bind(this)}>Hello</div>
    }
}

Should I use one over the other?

1
  • Specifically read Felipe Skinner's answer. Commented Jun 9, 2016 at 22:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.