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

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

export default class Parent extends Component {
    onRef = (ref) => {
        this.child = ref
    }

    clickcs = (e) => {
        this.child.myName()
    }
    render() {
        return(
            <View>
                <Child onRef={this.onRef} />
                <TouchableOpacity onPress={this.clickcs} >
            <Text>click</Text>
          </TouchableOpacity>
                
            </View>
        )
    }

}

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

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

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

 

posted @ 2022-09-05 17:58  龙卷风吹毁停车场  阅读(118)  评论(0)    收藏  举报