react-native 父函数组件调用类子组件的方法(实例详解)_React

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>)
    }
}

补充:下面看下React 函数式组件之父组件调用子组件的方法

前言&#

猜你喜欢

转载自blog.csdn.net/shengyin714959/article/details/130370785