Tri*_*iad 9 reactjs react-router
我一直在学习 React 和 Redux,并且注意到<Link>在使用 React-Router 时有两种以编程方式更改 URL 的方法(不使用 )。
一种方法是直接推送到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>
}
}
Run Code Online (Sandbox Code Playgroud)
另一种是推向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>
}
}
Run Code Online (Sandbox Code Playgroud)
我应该使用其中一种而不是另一种吗?
| 归档时间: |
|
| 查看次数: |
546 次 |
| 最近记录: |