網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
本文是小結(jié)類(lèi)文章,主要總結(jié)一下工作中遇到的父組件調(diào)用子組件方法。當(dāng)然除了用ref之外還有很多其他方式,本文僅僅列舉ref的方式。分別介紹父子組件都為class;父子組件都是hooks;父組件是class子組件是hooks;父組件是hooks,子組件是class的各種情況的調(diào)用方式。
父子組件都為class
父子組件都是class,父組件調(diào)用子組件方式比較傳統(tǒng),方法如下:
// 父組件
import React, {Component} from 'react';
export default class Parent extends Component {
render() {
return(
<div>
<Child onRef={this.onRef} />
<button onClick={this.click} >click</button>
</div>
)
}
onRef = (ref) => {
this.child = ref
}
click = (e) => {
this.child.myName()
}
}
//子組件
class Child extends Component {
componentDidMount(){ //必須在這里聲明,所以 ref 回調(diào)可以引用它
this.props.onRef(this)
}
myName = () => alert('my name is haorooms blogs')
render() {
return (<div>haorooms blog test</div>)
}
}
父子組件都為hooks
一般我們會(huì)結(jié)合useRef,useImperativeHandle,forwardRef等hooks來(lái)使用,官方推薦useImperativeHandle,forwardRef配合使用,經(jīng)過(guò)實(shí)踐發(fā)現(xiàn)forwardRef不用其實(shí)也是可以的,只要子組件把暴露給父組件的方法都放到useImperativeHandle里面就可以了。
/* FComp 父組件 */
import {useRef} from 'react';
import ChildComp from './child'
const FComp = () => {
const childRef = useRef();
const updateChildState = () => {
// changeVal就是子組件暴露給父組件的方法
childRef.current.changeVal(99);
}
return (
<>
<ChildComp ref={childRef} />
<button onClick={updateChildState}>觸發(fā)子組件方法</button>
</>
)
}
import React, { useImperativeHandle, forwardRef } from "react"
let Child = (props, ref) => {
const [val, setVal] = useState();
useImperativeHandle(ref, () => ({ // 暴露給父組件的方法
getInfo,
changeVal: (newVal) => {
setVal(newVal);
},
refreshInfo: () => {
console.log("子組件refreshInfo方法")
}
}))
const getInfo = () => {
console.log("子組件getInfo方法")
}
return (
<div>子組件</div>
)
}
Child = forwardRef(Child)
export default Child
父組件為class,子組件為hooks
其實(shí)就是上面的結(jié)合體。子組件還是用useImperativeHandle ,可以結(jié)合forwardRef,也可以不用。
// 父組件class
class Parent extends Component{
child= {} //主要加這個(gè)
handlePage = (num) => {
// this.child.
console.log(this.child.onChild())
}
onRef = ref => {
this.child = ref
}
render() {
return {
<ListForm onRef={this.onRef} />
}
}
}
// 子組件hooks
import React, { useImperativeHandle } from 'react'
const ListForm = props => {
const [form] = Form.useForm()
//重置方法
const onReset = () => {
form.resetFields()
}
}
useImperativeHandle(props.onRef, () => ({
// onChild 就是暴露給父組件的方法
onChild: () => {
return form.getFieldsValue()
}
}))
..............
父組件為hooks,子組件是class
這里其實(shí)和上面差不多,react主要dom省略,僅展示精華部分
//父組件hooks
let richTextRef = {};
// richTextRef.reseditorState();調(diào)用子組件方法
<RichText
getRichText={getRichText}
content={content}
onRef={ref => richTextRef = ref}
/>
//子組件class
componentDidMount = () => {
this.props.onRef && this.props.onRef(this);// 關(guān)鍵部分
}
reseditorState = (content) => {
this.setState({
editorState: content ||'-',
})
}
小結(jié)
本文主要是總結(jié),有些朋友在hooks或者class混合使用的時(shí)候,不清楚怎么調(diào)用子組件方法,這里總結(jié)一下,希望對(duì)各位小伙伴有所幫助。
原文鏈接:https://www.haorooms.com/post/react_class_hooks_dy
相關(guān)推薦
- 2022-04-18 Go中g(shù)routine通信與context控制實(shí)例詳解_Golang
- 2022-07-28 C++中操作符的前置與后置有什么區(qū)別_C 語(yǔ)言
- 2022-09-20 Python數(shù)字比較與類(lèi)結(jié)構(gòu)_python
- 2023-05-08 Python中Generators教程的實(shí)現(xiàn)_python
- 2022-09-04 關(guān)于python?DataFrame的合并方法總結(jié)_python
- 2022-10-15 Golang官方限流器庫(kù)實(shí)現(xiàn)限流示例詳解_Golang
- 2022-11-17 Python?Scala中使用def語(yǔ)句定義方法的詳細(xì)過(guò)程_python
- 2022-05-16 C++STL之vector模板類(lèi)詳解_C 語(yǔ)言
- 最近更新
-
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- 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)程分支