react 父组件调用子组件中的事件

import React, {Component} from ‘react‘;

export default class Parent extends Component {

  render() {

    return(

        <div> <Child onRef={this.onRef} />

        <button onClick={this.click} >click</button>

        </div>

    )

  }

  onRef = (ref) => {

    this.child = ref

  }

  click = (e) => {

    this.child.myName()

  }

}

class Child extends Component {

constructor(props){    super(props);    this.state = {value: ‘‘};}

  componentDidMount(){

    this.props.onRef(this)

  }

  myName = () => {

    this.setState({value:‘boonook‘});

  }

render(){    return(        <div>            我是子组件            <input value={this.state.value} type="text"/>        </div>    )}

}

原文地址:https://www.cnblogs.com/boonook/p/9598470.html

时间: 2024-08-30 10:51:42

react 父组件调用子组件中的事件的相关文章

angular 中父组件调用子组件的方法

angular中 父组件调用子组件的方法 -- 使用 @ViewChild 装饰器修饰子组件,获取方法,调用 除此之外 ViewChild 还可以获取 DOM ,操作 DOM , 详见: https://www.cnblogs.com/monkey-K/p/11567098.html 1. html 中使用 #var 方式标记 子组件 <div style="border: 1px solid red"> <p>子组件:</p>> <ap

react ,父子调用子组件的方法与子组件调用父组件的方法

1.父组件调用子组件的方法给子组件的标签 定义一个属性,例如 ref="zizu" ------------- 父组件通过,this.refs.biaoji.dream('哈哈') //调用子组件的dream方法 2.子组件调用父组件的方法 2.1.首先父组件需要通过给子组件自定义属性,把方法传递给子组件.2.2.子组件通过this.props 接收父组件的方法,this.props.方法名称().这样就可以调用父组件的方法了 原文地址:https://www.cnblogs.com/

VUE 父组件调用子组件弹窗

想搞一个新增编辑弹窗,和列表页面分开 先来一个父组件调用子组件弹窗的demo 父组件 <template> <div> <el-button @click="show">按钮</el-button> <!-- 新增编辑弹框子组件 --> <add-or-update :addOrUpdateVisible="addOrUpdateVisible" @changeShow="showAddOr

父框架调用子框架中内容的问题记录

有一个项目需要用框架来完成,在编码中发现父框架调用子框架中内容的实现有兼容性问题,特此记录下来! <iframe id="preview" name="preview" src="bill.php?page=preview" frameborder="0" width="100%"></iframe> 这个是我的框架,我准备操作框架中的DOM.所以我使用了, container= p

React父组件调用子组件

通点击父组件按钮调用子组件的方法 父组件: import React, {Component} from 'react'; import ChildComponent from './child'; export default class ParentComponent extends Component { render() { return( <div> <ChildComponent onRefChild={this.onRefChild} /> <button on

父组件调用子组件中的方法- this.$refs.xxx.子组件方法();

子组件中有一个说的方法 在父组件中去调用当你点击的时候 去调用子组件中的方法 fu.vue 在父组件的方法中调用子组件的方法,很重要 this.$refs.mychild.parentHandleclick(); { <template> <div> <button @click="clickParent">点击 调用子组件</button> <child ref="mychild"></child&

vue中父组件调用子组件函数

用法: 子组件上定义ref="refName",  父组件的方法中用 this.$refs.refName.method 去调用子组件方法 详解: 父组件里面调用子组件的函数,父组件先把函数/方法以属性形式传给子组件:那么就需要先找到子组件对象 ,即  this.$refs.refName. 然后再进行调用,也就是 this.$refs.refName.method 如下: 子组件: <template> <div> childComponent </di

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

vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如: 父组件: <template> <div @click="fatherMethod"> <child ref="child"></child> </div> </template> <script> import child from '~/componen

vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><div id="app"></div></body><script src="node_modules/vue/

父组件调用子组件的方法

我们都知道通过$ref可以获取到某个DOM,但是它也可以用来获取子组件的实例,调用子组件的方法 父组件 子组件 原文地址:https://www.cnblogs.com/xianhao/p/10475314.html