【问题标题】:( React.js ) Can I use a function thats placed in a different component?( React.js ) 我可以使用放置在不同组件中的函数吗?
【发布时间】:2016-02-01 19:18:03
【问题描述】:

我有主组件。例如,现在我基于 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>
    }
})

【问题讨论】:

  • 你可以在组件之间传递函数和props &lt;ComponentOne callback={this.function}&gt;

标签: meteor reactjs flow-router


【解决方案1】:

主组件

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

组件一

var ComponentOne = React.createClass({   
    render: function() {
        this.props.myFunction();
        return <h1>Hello World</h1>;
    }
});

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 2021-06-12
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 2011-05-06
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多