Прошу вас, пожалуйста. Я просто изучаю Reactjs и придерживаюсь точки.Как передать реквизиты (оба состояния и функции) на компонент дочернего маршрутизатора
приложение-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()
не стреляет. Как передать реквизит на {this.props.children}
(т. Е. Аудитория или спикер) на APP?
обновление:
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'. –