React父组件调用子组件方法

代码块

import React, {Component} from 'react';

export default class Root extends Component {
    render() {
        return(
            <div>
                <Child onRef={this.onRef} />
                <Button onClick={this.onClick} >通信</Button>
            </div>
        )
    }

    onRef = (ref) => {
        this.child = ref
    }

    onClick = (e) => {
        this.child.onClick()
    }

}

class Child extends Component {
    componentDidMount(){
        this.props.onRef(this)
    }

    onClick = () => console.info("父子通信OK")

    render() {
        return (<div>父子通信</div>)
    }
}
感谢您的阅读,本文由 Seven 版权所有。如若转载,请注明出处:Seven(http://qsw.red/2018/05/30/React父组件调用子组件方法/
EditText输入手机号自动带空格