gpt4 book ai didi

javascript - react-router - 将 Prop 传递给处理程序组件

转载 作者:IT老高 更新时间:2023-10-28 11:05:56 26 4
gpt4 key购买 nike

我的 React.js 应用程序具有以下结构,使用 React Router :

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);
});

我想将一些属性传递给 Comments零件。

(通常我会这样做 <Comments myprop="value" />)

使用 React Router 最简单和正确的方法是什么?

最佳答案

如果您不想编写包装器,我想您可以这样做:

class Index extends React.Component { 

constructor(props) {
super(props);
}
render() {
return (
<h1>
Index - {this.props.route.foo}
</h1>
);
}
}

var routes = (
<Route path="/" foo="bar" component={Index}/>
);

关于javascript - react-router - 将 Prop 传递给处理程序组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com