我们都知道在 react中,若要在父组件调用子组件的方法,通常我们会采用在父组件定义一个方法,作为props转给子组件,然后执行该方法,可以获取到子组件传回的参数以得到我们的目的。

显而易见,这个执行是需要去主动触发的。

那有没有一种方式,方法在子组件定义好,可以直接在父组件去调用呢?

答案是必然的。

上代码^ - ^

import React, {Component} from "react";
import { Button } from "antd"; //父组件
export default class Parent extends Component {
render() {
return(
<div>
          <p>这是父组件</p>
<Child triggerRef={this.bindRef} />
<Button type="primary" onClick={this.btnClick} >click</Button>
</div>
)
} bindRef = ref => { this.child = ref } btnClick = () => {
this.child.getValuefromChild()
} }
//子组件
class Child extends Component {
componentDidMount(){
this.props.triggerRef(this)
}
  //这是子组件的方法
getValuefromChild = () => console.log("this is child value.") render() {
return <div>这是子组件</div>
}
}

是不是瞬间就风清月朗了~~

react中直接调用子组件的方法(非props方式)的更多相关文章

  1. vue父组件中调用子组件的方法

    Vue项目中如何在父组件中直接调用子组件的方法: 方案一:通过ref直接调用子组件的方法: //父组件中 <template> <div> <Button @click= ...

  2. vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

    在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> < ...

  3. vue中父组件调用子组件的方法

    原文地址 文章目录 什么是组件? 使用组件 组件 什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自 ...

  4. React 父组件调用子组件的方法

    父组件调用子组件的方法 React v16.3.0 及以后版本使用 import React, {Component} from 'react'; export default class Paren ...

  5. Vue父组件调用子组件的方法

    vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如: 父组件: <template> <div @click ...

  6. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

  7. Vue 父组件调用子组件的方法

    qwq  前两天看了下vue,父子组件方法的调用,怕忘记,所以做个小记录. 一.父组件调用子组件的方法 1.父组件 <template> <div id="rightmen ...

  8. Vue3 父组件调用子组件的方法

    Vue3 父组件调用子组件的方法 // 父组件 <template> <div> 父页面 <son-com ref="sonRef"/> < ...

  9. Angular 中的 dom 操作(ViewChild)以及父子组件中通过 ViewChild 调用子组件的方法

    <app-header #header></app-header> <div #myBox> 我是一个dom节点 </div> <button ( ...

随机推荐

  1. 【转载】SeleniumIDE入门

    http://www.open-open.com/lib/view/open1452488109558.html

  2. ABAP-FI常用BAPI

    总帐会计:  (比较简单全部测试通过,关帐时使用) Line item of document for ledger with summary table GL F: BAPI_GLX_GETDOCI ...

  3. Appium典型问题处理

    1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新版本a ...

  4. _event

      EventId  事件ID 请使用大于100的ID EventName 事件的名称,用于游戏中各种提示 NoticeText  事件开始时的弹窗内容 GossipText  功能宝石等菜单内容 Z ...

  5. _event_active

    EventId 事件ID GUID 对应creature或gameobject表中 guid,正数为生物,负数为物体 ActiveFlag 生物或物体激活时的flag,通常为0 NoticeText ...

  6. Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp

    E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. pyqt 不规则形状窗口显示

    #coding=utf- import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QApplicatio ...

  8. centos7 查看防火墙状态

    [root@xxx ~]# firewall-cmd --state not running

  9. NET Core 指令启动

    ASP.NET Core 是新一代的 ASP.NET,早期称为 ASP.NET vNext,并且在推出初期命名为ASP.NET 5,但随着 .NET Core 的成熟,以及 ASP.NET 5的命名会 ...

  10. python模块(4)

    re正则 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回(没有group()方法) re.splitall 以匹配到的字符 ...