React(onRef)父组件调用子组件中的方法

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

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

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

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

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

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

你可能感兴趣的:(React,js,js_实例)