Saturday, January 27, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...