react-native 类组件调用函数组件里面的方法
作者:互联网
父组件 class 类组件, 关键代码我用红色部部分标记
import React from "react"; import { View,Text} from "react-native"; import HomeModalChilder from "../../components/homeModal/index"; export default class Modal extends React.Component{ constructor(props) { super(props); this.containerRef = React.createRef() } clikbox=()=>{ this.containerRef.current.showModal() console.log(this.containerRef.current, '-----') } render(){ return( <View> <Text onPress={this.clikbox}>Modal</Text> <HomeModalChilder ref={this.containerRef}></HomeModalChilder> </View> ) } }
函数式子组件
import React, { forwardRef, useImperativeHandle } from "react"; import { Text, View } from "react-native"; const APP = forwardRef((props, ref) => { const showModal = () => { alert('调用成功') } useImperativeHandle(ref, () => ({ showModal, })) return ( <View> <Text>我是函数式组件</Text> </View> ); }) export default APP;
标签:React,containerRef,调用函数,react,组件,import,native 来源: https://www.cnblogs.com/tlfe/p/16650905.html