我的應用程序是ES6 React應用程序與react-router。我想稍後將用戶重定向到其他頁面。這裏是我的陣營組成:未指定所需的上下文`router`。檢查`RoutingContext`的渲染方法
import React from 'react'
import { Navigation } from 'react-router'
export default class Component extends React.Component {
render() {
return (
<div>Component content</div>
)
}
componentDidMount() {
setTimeout(() => {
// TODO: redirect to homepage
console.log('redirecting...');
this.context.router.transitionTo('homepage');
}, 1000);
}
}
Component.contextTypes = {
router: React.PropTypes.func.isRequired
}
和應對路由器的路由表:
render(
<Router>
<Route path='/' component={ App }>
<IndexRoute component={ Component } />
</Route>
</Router>
, document.getElementById('app-container'));
的問題是,「路由器」屬性不會傳遞到組件。 Chrome瀏覽器控制檯的內容是:
Warning: Failed Context Types: Required context `router` was not specified in `Component`. Check the render method of `RoutingContext`.
redirecting...
Uncaught TypeError: Cannot read property 'transitionTo' of undefined
陣營的版本是0.14.2,反應路由器的版本是1.0.0-RC4 我在哪裏出錯?
我跟着從http://stackoverflow.com/questions/32033247/react-router-transitionto-is-not-a-function/33008706#33008706創建構造函數和更改值建議'路由器'上下文類型,但結果是'transitionTo'仍然不可用。 – taydakov
PayPal開發者有類似的問題,但它被關閉https://github.com/paypal/react-engine/issues/82 – taydakov