網站首頁 編程語言 正文
前言
本文是小結類文章,主要總結一下工作中遇到的父組件調用子組件方法。當然除了用ref之外還有很多其他方式,本文僅僅列舉ref的方式。分別介紹父子組件都為class;父子組件都是hooks;父組件是class子組件是hooks;父組件是hooks,子組件是class的各種情況的調用方式。
父子組件都為class
父子組件都是class,父組件調用子組件方式比較傳統,方法如下:
// 父組件
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 回調可以引用它
this.props.onRef(this)
}
myName = () => alert('my name is haorooms blogs')
render() {
return (<div>haorooms blog test</div>)
}
}
父子組件都為hooks
一般我們會結合useRef,useImperativeHandle,forwardRef等hooks來使用,官方推薦useImperativeHandle,forwardRef配合使用,經過實踐發現forwardRef不用其實也是可以的,只要子組件把暴露給父組件的方法都放到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}>觸發子組件方法</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
其實就是上面的結合體。子組件還是用useImperativeHandle ,可以結合forwardRef,也可以不用。
// 父組件class
class Parent extends Component{
child= {} //主要加這個
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
這里其實和上面差不多,react主要dom省略,僅展示精華部分
//父組件hooks
let richTextRef = {};
// richTextRef.reseditorState();調用子組件方法
<RichText
getRichText={getRichText}
content={content}
onRef={ref => richTextRef = ref}
/>
//子組件class
componentDidMount = () => {
this.props.onRef && this.props.onRef(this);// 關鍵部分
}
reseditorState = (content) => {
this.setState({
editorState: content ||'-',
})
}
小結
本文主要是總結,有些朋友在hooks或者class混合使用的時候,不清楚怎么調用子組件方法,這里總結一下,希望對各位小伙伴有所幫助。
原文鏈接:https://www.haorooms.com/post/react_class_hooks_dy
- 上一篇:Qt實現柵格布局效果_C 語言
- 下一篇:AJAX跨域問題解決方案詳解_AJAX相關
相關推薦
- 2023-12-06 EmpMapper is not known to the MapperRegistry
- 2022-09-18 Go語言實現文件上傳_Golang
- 2022-04-09 Android項目中gradle的執行流程_Android
- 2023-07-02 Python+streamlit實現輕松創建人事系統_python
- 2022-04-20 C語言特殊符號的補充理解_C 語言
- 2022-12-13 詳解如何魔改Retrofit實例_Android
- 2022-03-30 C++?Qt?QColorDialog使用方法_C 語言
- 2022-12-07 C++?Boost?Container庫示例詳細講解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支