我使用React Router为我的React.js应用程序提供了以下结构:
var Dashboard = require('./Dashboard');
var Comments = require('./Comments');
var Index = React.createClass({
render: function () {
return (
<div>
<header>Some header</header>
<RouteHandler />
</div>
);
}
});
var routes = (
<Route path="/" handler={Index}>
<Route path="comments" handler={Comments}/>
<DefaultRoute handler={Dashboard}/>
</Route>
);
ReactRouter.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
});
Run Code Online (Sandbox Code Playgroud)
我想将一些属性传递给Comments组件.
(通常我会这样做<Comments myprop="value" />)
使用React Router,最简单,最正确的方法是什么?
在组件1的某个地方,我有这个:
if (redirectToReferrer) {
return <Redirect to={{
pathname: "/test/new",
state: { event: "test" }
}} />;
}
Run Code Online (Sandbox Code Playgroud)
在组件2中,我有以下内容:
constructor(props){
super(props);
console.log(this.props.location) //undefined
}
Run Code Online (Sandbox Code Playgroud)
根据文档,这应该工作。
可以通过重定向到组件中的this.props.location.state访问状态对象。然后,可以通过路径名“ / login”指向的Login组件中的this.props.location.state.referrer访问此新的引荐来源关键字(不是特殊名称)。
为什么this.props.location未定义?