zoukankan      html  css  js  c++  java
  • 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>
    )
    }

    }

  • 相关阅读:
    Fibonacci数列2
    足球队
    网页导航
    Catenyms
    某种密码
    大逃亡
    球的序列
    圆内三角形统计
    最小平方数

  • 原文地址:https://www.cnblogs.com/boonook/p/9598470.html
Copyright © 2011-2022 走看看