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 函数式组件之父组件调用子组件的方法
前言&#