如何使用 ReactJS(和 react-router)将 props 传递到 react-router
How to pass props into a react-router with ReactJS (and react-router)
我有这段代码可以在不同的组件之间切换。
我想以一种简单的方式将值传递给不同的组件,就像这样;
<Route path="student" component={Student} websocket={self.websocket}/>
有没有简单的方法可以做到这一点?我看过克隆 类 等,但我不认为这些答案是 "good enough" 我猜。我没有找到类似的情况,希望有人能回答我的问题。
代码在"full";
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="student" component={Student}/>
<Route path="about" component={About}/>
<Route path="login" component={LoginForm}/>
<Route path="oauth" component={Knapp}/>
</Route>
</Router>, document.getElementById("container") );
你可以这样做
class Example extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<h1>
Checking props - {this.props.route.foo}
</h1>
);
}
}
var routes = (
<Route path="/" foo="bar" component={Example}/>
);
你也可以看看here如何用另一种方式做到这一点
希望对你有所帮助
谢谢
我有这段代码可以在不同的组件之间切换。 我想以一种简单的方式将值传递给不同的组件,就像这样;
<Route path="student" component={Student} websocket={self.websocket}/>
有没有简单的方法可以做到这一点?我看过克隆 类 等,但我不认为这些答案是 "good enough" 我猜。我没有找到类似的情况,希望有人能回答我的问题。
代码在"full";
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="student" component={Student}/>
<Route path="about" component={About}/>
<Route path="login" component={LoginForm}/>
<Route path="oauth" component={Knapp}/>
</Route>
</Router>, document.getElementById("container") );
你可以这样做
class Example extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<h1>
Checking props - {this.props.route.foo}
</h1>
);
}
}
var routes = (
<Route path="/" foo="bar" component={Example}/>
);
你也可以看看here如何用另一种方式做到这一点
希望对你有所帮助 谢谢