我有mainComponent。现在,例如,基于1> 2渲染两个不同的组件。现在我如何使用另一个组件上不同组件的数据和功能
ComponentOne = React.createClass({
render() {
/* use theFunctionImTryingToRun -output "a" */
}
})
mainComponent = React.createClass({
var x = ["a","b","c"];
theFunctionImTryingToRun: function(){
console.log(x[0]);
},
mainRender: function(){
if (1<2) {
return (<ComponentOne /> );
} else {
return (<ComponentTwo />);
}
}
render() {
return <div> {this.mainRender()} </div>
}
})
答案 0 :(得分:0)
MainComponent
var MainComponent = React.createClass({
theFunctionImTryingToRun: function() {
var x = ["a","b","c"];
console.log(x[0]);
},
render: function() {
return 1 < 2 ?
<ComponentOne myFunction={this.theFunctionImTryingToRun} /> :
<ComponentTwo />;
}
});
ComponentOne的
var ComponentOne = React.createClass({
render: function() {
this.props.myFunction();
return <h1>Hello World</h1>;
}
});