【发布时间】:2016-10-28 13:20:04
【问题描述】:
请多多包涵。我刚刚学习 Reactjs 并停留在一个点上。
app-client.js
ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={APP}>
<IndexRoute component={Audience}/>
<Route path="speaker" component={Speaker}/>
<Route path="board" component={Board}/>
</Route>
</Router>
), document.getElementById('react-container'));
APP.js
var APP = React.createClass({
getInitialState() {
return {
status: 'disconnected',
title: ''
}
},
emit(eventName, payload) {
this.socket.emit(eventName, payload);
},
render() {
return (
<div>
<Header title={this.state.title} status={this.state.status}/>
{this.props.children}
</div>
);
}
});
Audience.js:
var Audience = React.createClass({
render() {
return (<h1>Audience: {this.props.title}</h1>);
}
});
页面显示所有组件,但this.props.title 未显示在页面中,emit() 未触发。如何在APP上将道具传递给{this.props.children}(即Audience或Speaker)?
更新:
APP.js 渲染():
render() {
const _this = this;
return (
<div>
<Header title={this.state.title} status={this.state.status}/>
{ React.children.map(this.props.children, (child, index) => {
//Get props of child
// const childProps = child.props;
//do whatever else you need, create some new props, change existing ones
//store them in variables
return React.cloneElement(child, {
// ...childProps, //these are the old props if you don't want them changed
// ...someNewProps,
// someOldPropOverwritten, //overwrite some old the old props
..._this.state,
emit: _this.emit
});
})
}
</div>
);
}
});
【问题讨论】:
-
使用
React.children.map(this.props.children...和大写Children。
标签: javascript reactjs react-router