8

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?

Jordan
  • 574
  • 1
  • 5
  • 15

0 Answers0