react this.refs.childMethod is not a function

react this.refs.method is not a function

父组件

 <Menu.Item key="change_pwd">
    <ChangePwd ref="Changepwd" />
 </Menu.Item>
                  
                  
  handleMenuClick = (item) => {
    console.log(item.key)
    if (item.key == "logout") {
      this.handleLogout()
    }
    if (item.key == "change_pwd") {
      console.log(this.refs.Changepwd)
      // this.refs.Changepwd.childMethod()
    }
  }

子组件

  childMethod() {
    alert("组件之间通信成功");
  }

输出react this.refs.childMethod is not a function

为啥找不到子组件的方法呢
打印出来的this.refs.Changepwd如图并没有自己定义的method

clipboard.png

阅读 3.3k
2 个回答

this.refs.Changepwd 是获取到的这个实体 Dom, 并不是一个方法。

你的ChangePwd是通过Class声明的么?看看文档

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题