react父组件调用子组件方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/koufulong/article/details/80931654
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>)
    }
}

猜你喜欢

转载自blog.csdn.net/koufulong/article/details/80931654