react-native 父函数组件调用类子组件的方法

import React, {Component} from 'react';
import {Text, View,  TouchableOpacity} from 'react-native';

//
let child
onRefbbb = (ref) => {
    child = ref
}
clickccc = () => {
    child.myName()
}
const Parent =()=> {
        return(
            <View>
                <Child onRefaaa={onRefbbb} />
                <TouchableOpacity onPress={()=>clickccc()}>
                    <Text>onClick</Text>
                </TouchableOpacity>
            </View>
        )
}
export default Parent

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

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

    render() {
        return (<View></View>)
    }
}

 

posted @ 2022-09-09 10:18  龙卷风吹毁停车场  阅读(137)  评论(0)    收藏  举报