網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
react-native?父函數(shù)組件調(diào)用類子組件的方法(實(shí)例詳解)_React
作者:龍卷風(fēng)吹毀停車場(chǎng) ? 更新時(shí)間: 2022-11-10 編程語(yǔ)言react-native 父函數(shù)組件調(diào)用類子組件的方法,代碼如下所示:
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>)
}
}
補(bǔ)充:下面看下React 函數(shù)式組件之父組件調(diào)用子組件的方法
前言:
最近做一個(gè)React項(xiàng)目,所有組件都使用了函數(shù)式組件,遇到一個(gè)父組件調(diào)用子組件方法的問題,讓我好一陣頭疼。
我們都知道,React 中子組件調(diào)用父組件方法,使用 props 傳遞過來(lái)就可以在子組件中使用。但是父組件如何調(diào)用子組件方法呢?請(qǐng)看下面代碼:
第一步:
在父組件中,使用 useRef 創(chuàng)建一個(gè) ref
import { useRef } from 'react'
// 父組件中代碼
const FCom = () => {
const cRef = useRef(null);
return (
<div>
<ChildCom ref={cRef} />
</div>
)
}
第二步:
子組件中代碼:(使用了 forwardRef,useImperativeHandle)
import { forwardRef, useImperativeHandle } from 'react'
const ChildCom = forwardRef(({
// 這里是一些props參數(shù)
}, ref) => {
useImperativeHandle(ref, () => ({
getData: getData,
otherFun: otherFun
}))
function getData() {
// to do something
}
function otherFun() {
console.log('這是其他方法')
}
return (
<div>子組件</div>
)
})
第三步:
此時(shí),在父組件中就可以隨心所欲的調(diào)用子組件中的方法了!
import { useRef } from 'react'
// 修改父組件中代碼
const FCom = () => {
const cRef = useRef(null);
const handleClick = () => {
cRef.current && cRef.current.getData()
}
return (
<div>
<ChildCom ref={cRef} />
</div>
)
}
原文鏈接:https://www.cnblogs.com/tlfe/p/16671868.html
相關(guān)推薦
- 2023-02-12 Android?onbackpressed實(shí)現(xiàn)返回鍵的攔截和彈窗流程分析_Android
- 2022-06-08 Spring Cloud Nacos NacosWatch
- 2022-04-10 微信小程序 base64 圖片 canvas 畫布 drawImage 實(shí)現(xiàn)
- 2022-12-02 React?Hook中的useState函數(shù)的詳細(xì)解析_React
- 2022-05-15 require和import的區(qū)別詳解
- 2022-04-21 R語(yǔ)言繪制帶ErrorBar的分組條形圖代碼的分享_R語(yǔ)言
- 2022-12-03 Python學(xué)習(xí)之列表和元組的使用詳解_python
- 2022-11-25 淺析Golang中的內(nèi)存逃逸_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支