react父組件調(diào)用子組件的方式匯總
前言
本文是小結(jié)類文章,主要總結(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來使用,官方推薦useImperativeHandle,forwardRef配合使用,經(jīng)過實(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ì)各位小伙伴有所幫助。
到此這篇關(guān)于react父組件調(diào)用子組件的文章就介紹到這了,更多相關(guān)react父組件調(diào)用子組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react-native 封裝選擇彈出框示例(試用ios&android)
本篇文章主要介紹了react-native 封裝選擇彈出框示例(試用ios&android),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Redux thunk中間件及執(zhí)行原理詳細(xì)分析
redux的核心概念其實(shí)很簡(jiǎn)單:將需要修改的state都存入到store里,發(fā)起一個(gè)action用來描述發(fā)生了什么,用reducers描述action如何改變state tree,這篇文章主要介紹了Redux thunk中間件及執(zhí)行原理分析2022-09-09
解決React報(bào)錯(cuò)Cannot assign to 'current'
這篇文章主要為大家介紹了React報(bào)錯(cuò)Cannot assign to 'current' because it is a read-only property的解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn)詳細(xì)講解
這篇文章主要介紹了React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn),嵌套路由原則是可以無限嵌套,但是必須要讓使用二級(jí)路由的一級(jí)路由匹配到,否則不顯示,需要的朋友可以參考一下2022-09-09
圖文示例講解useState與useReducer性能區(qū)別
這篇文章主要為大家介紹了useState與useReducer性能區(qū)別圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
ahooks正式發(fā)布React?Hooks工具庫(kù)
這篇文章主要為大家介紹了ahooks正式發(fā)布值得擁有的React?Hooks工具庫(kù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

