Reactjs 函数不';t在react js中从一个类调用另一个类

Reactjs 函数不';t在react js中从一个类调用另一个类,reactjs,Reactjs,我是react js的新手,所以我对这种类型的错误感到抱歉,这里我创建了两个类,Test1和Test2,我想从类1调用function\u twofunction,但它不起作用,有人能帮我解决这个问题吗,这是我的完整代码 class Test1 extends React.Component { function_one() { <Test2 function_two={this.function_two} /> } render() {

我是react js的新手,所以我对这种类型的错误感到抱歉,这里我创建了两个类,Test1和Test2,我想从类1调用
function\u two
function,但它不起作用,有人能帮我解决这个问题吗,这是我的完整代码

class Test1 extends React.Component {
    function_one() {
        <Test2 function_two={this.function_two} />
    }
    render() {
        return (
          <h1>{this.function_one}</h1>
        );
    }
}

class Test2 extends React.Component {
  function_two() {
      return "Funtion 2";
  }
}

//CallCRUD

ReactDOM.render(
  <Test1 />, document.getElementById('root')
);
类Test1扩展了React.Component{ 函数_one(){ } render(){ 返回( {this.function_one} ); } } 类Test2扩展了React.Component{ 函数二(){ 返回“功能2”; } } //CallCRUD ReactDOM.render( ,document.getElementById('root')) ); 两件事:

  • 您忘记调用函数:
    this.function\u one
    应该是
    this.function\u one()
  • 您没有在
    函数\u one
    中返回任何内容(您忘记了返回语句)
  • 工作修复:

    类Test1扩展了React.Component{ 函数二(原点){ log('Hello,from'+origin) } 函数_one(){ 返回 } render(){ 返回( {this.function_one()} ); } } 类Test2扩展了React.Component{ componentDidMount(){ this.props.function_two('Test2')) } 渲染=()=> } ReactDOM.render( ,document.getElementById('root')) );
    
    
    您到底想实现什么目标?有多种方法可以做到这一点。您想将状态数据从
    Test2
    传递到
    Test1
    ?谢谢,但是我们不能从Test2类调用函数_two()吗?在我给您的代码中,函数在Test2中被调用,
    this.props.function_two('Test2')
    调用itThanks,我可以在console.log中看到它正在工作,但当我刚刚返回该字符串时,标签上没有打印,你能帮我吗