react父组件调用子组件方法

2019-06-21  本文已影响0人  Volon
import React, {Component} from 'react';
import {Text, View,  TouchableOpacity} from 'react-native';

export default class Parent extends Component {
    render() {
        return(
            <View>
                <Child onRef={this.onRef} />
                <TouchableOpacity onClick={this.click} >
                <Text>click</Text>
            </View>
        )
    }

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

    click = (e) => {
        this.child.myName()
    }

}

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

     myName = () =>{
      alert(11111111111111)
  
     }

    render() {
        return (<View></View>)
    }
}
上一篇下一篇

猜你喜欢

热点阅读