Saturday, 27 January 2018

reactjs - React Router: browserHistory.push() vs this.context.router.push()

I've been learning React along with Redux, and have noticed two ways to change the URL programmatically (not using ) 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
Hello

}
}


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
Hello

}
}


Should I use one over the other?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...