我有一些简单的组件,我想使用router.transitionTo函数。我在这做错了什么:
const { Router,
Route,
IndexRoute,
Redirect,
Link,
IndexLink
} = ReactRouter
const Person = React.createClass({
render() {
return (
<div>
hihihi
</div>
)
}
})
const App = React.createClass({
clickHandler() {
this.context.router.transitionTo('/person/23')
},
render() {
return (
<div>
<h1 onClick={this.clickHandler}>React Router Playground</h1>
{this.props.children}
</div>
)
}
})
App.contextTypes = {
router: React.PropTypes.object.isRequired,
}
React.render((
<Router>
<Route path="/" component={App}/>
<Route path="/person/:id" component={Person}/>
</Router>
), document.getElementById('app'))
答案 0 :(得分:3)
在App组件中,您有方法:
clickHandler() {
this.context.router.transitionTo('/person/23')
},
请将其更改为:
clickHandler() {
this.context.router.push('/person/23')
},
工作代码:http://codepen.io/anon/pen/xVOabg
这是因为react-router的实际版本使用router.push
来更改路由