React router transitionTo,.substring不是一个函数

时间:2016-03-11 08:58:58

标签: reactjs react-router

我有一些简单的组件,我想使用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'))

http://codepen.io/anon/pen/MyerXj?editors=001

1 个答案:

答案 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来更改路由