admin管理员组文章数量:1313001
My app is ES6 React application with react-router. I want to redirect user to a different page after a small delay. Here is my React ponent:
import React from 'react'
import { Navigation } from 'react-router'
export default class Component extends React.Component {
render () {
return (
<div>Component content</div>
)
}
ponentDidMount () {
setTimeout(() => {
// TODO: redirect to homepage
console.log('redirecting...');
this.context.router.transitionTo('homepage');
}, 1000);
}
}
Component.contextTypes = {
router: React.PropTypes.func.isRequired
}
And react-router routing table:
render(
<Router>
<Route path='/' ponent={ App }>
<IndexRoute ponent={ Component } />
</Route>
</Router>
, document.getElementById('app-container'));
The issue is that 'router' property is not passed into the ponent. Chrome console's content is:
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
React version is 0.14.2, react-router version is 1.0.0-rc4 Where do I make mistake?
My app is ES6 React application with react-router. I want to redirect user to a different page after a small delay. Here is my React ponent:
import React from 'react'
import { Navigation } from 'react-router'
export default class Component extends React.Component {
render () {
return (
<div>Component content</div>
)
}
ponentDidMount () {
setTimeout(() => {
// TODO: redirect to homepage
console.log('redirecting...');
this.context.router.transitionTo('homepage');
}, 1000);
}
}
Component.contextTypes = {
router: React.PropTypes.func.isRequired
}
And react-router routing table:
render(
<Router>
<Route path='/' ponent={ App }>
<IndexRoute ponent={ Component } />
</Route>
</Router>
, document.getElementById('app-container'));
The issue is that 'router' property is not passed into the ponent. Chrome console's content is:
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
React version is 0.14.2, react-router version is 1.0.0-rc4 Where do I make mistake?
Share Improve this question edited Nov 10, 2015 at 2:49 zaaath asked Nov 9, 2015 at 19:39 zaaathzaaath 7779 silver badges18 bronze badges 2- I followed advice from stackoverflow./questions/32033247/… created constructor and changed value of 'router' context type, but the result is 'transitionTo' is still not available. – zaaath Commented Nov 9, 2015 at 19:46
- PayPal developers had similar issue but it was closed github./paypal/react-engine/issues/82 – zaaath Commented Nov 9, 2015 at 19:47
3 Answers
Reset to default 4Im not a react-router expert by any means, but I had the same issue earlier today. I am using React 0.14.2 and React-Router 1.0 (this just came out in the last couple of days, if not more recently). While debugging I noticed that the props on the React ponent includes history (the new style of navigation - https://github./rackt/react-router/blob/master/docs/guides/basics/Histories.md)
I am also using TypeScript, but my code looks like the following:
import React = require('react');
import Header = require('./mon/header.tsx');
var ReactRouter = require('react-router');
interface Props extends React.Props<Home> {
history: any
}
class Home extends React.Component<Props, {}> {
render(): JSX.Element {
return (
<div>
<Header.Header MenuItems={[]} />
<div className="jumbotron">
<h1>Utility</h1>
<p>Click on one of the options below to get started...</p>
{<a className="btn btn-lg" onClick={() => this.props.history.pushState(null, '/remoteaccess') }>Remote Access</a>}
{<a className="btn btn-lg" onClick={() => this.props.history.pushState(null, '/bridge') }>Bridge</a>}
</div>
</div>
);
}
}
module.exports = Home;
I'm inclined to say that this.context.router
doesn't exist anymore. I've run into the same problem and it looks like we're supposed to implement these features using this.context.history
as well as this.context.location
. If I get something working, I'll try updating this response.
See the 1.0.0 upgrade guide and use this.props.history
with pushState
or replaceState
. context
is used for other ponents (not route ponents).
From the upgrade guide :
// v1.0
// if you are a route ponent...
<Route ponent={Assignment} />
var Assignment = React.createClass({
foo () {
this.props.location // contains path information
this.props.params // contains params
this.props.history.isActive('/pathToAssignment')
}
})
本文标签:
版权声明:本文标题:javascript - Required context `router` was not specified. Check the render method of `RoutingContext` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741826215a2399657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论