網(wǎng)站首頁 編程語言 正文
React Hook 父子組件相互調(diào)用函數(shù)
1.子組件調(diào)用父組件函數(shù)方法
//父組件
let Father=()=>{
?? ?let getInfo=()=>{
?? ??? ?
?? ?}
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<Children?
?? ??? ??? ??? ?getInfo={getInfo}
?? ??? ??? ?/>
?? ??? ?</div>
?? ?}
}
//子組件
let Children=(param)=>{
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<span onClick={param.getInfo}>調(diào)用父組件函數(shù)</span>
?? ??? ?</div>
?? ?}
}
子組件調(diào)用父組件函數(shù),可以向父組件傳參,刷新父組件信息
2.父組件調(diào)用子組件函數(shù)方法
//父組件
//需要引入useRef
import {useRef} from 'react'
let Father=()=>{
?? ?const childRef=useRef();
?? ?let onClick=()=>{
?? ??? ?childRef.current.getInfo();
?? ?}
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<Children?
?? ??? ??? ??? ?ref={childRef}
?? ??? ??? ?/>
?? ??? ??? ?<span onClick={onClick}>調(diào)用子組件函數(shù)</span>
?? ??? ?</div>
?? ?}
}
//子組件?
//需要引入useImperativeHandle,forwardRef
import {useImperativeHandle,forwardRef} from 'react'
let Children=(ref)=>{
?? ?useImperativeHandle(ref, () => ({
? ? ? ? getInfo:()=>{
? ? ? ? ? ? //需要處理的數(shù)據(jù)
? ? ? ? }
? ? }))
?? ?return ()=>{
?? ??? ?<div></div>
?? ?}
}
Children = forwardRef(Children);
useImperativeHandle 需要配合著 forwardRef 使用,要不就會出現(xiàn)以下警告
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
React Hook 父子組件傳值
我司現(xiàn)在技術(shù)棧是react,用的是開箱即用的pro,我個人習(xí)慣用函數(shù)式組件,所以用hook比較多。現(xiàn)在寫個父子組件傳值的示例,希望能幫助到你。
父組件
import React, { useState,createContext} from "react";
import Counter from './index1'
const myContext = createContext();
function App() {
? const [count, setCount] = useState(0);
? return (
? ? <div>
? ? ? <h4>我是父組件</h4>
? ? ? <p>點擊了 {count} 次!</p>
? ? ? <button
? ? ? ? onClick={() => {
? ? ? ? ? setCount(count + 1);
? ? ? ? }}
? ? ? >
? ? ? ? 點我
? ? ? </button>
? ? ? {/* 關(guān)鍵代碼 */}
? ? ? {/* 提供器 */}
? ? ? <myContext.Provider value={count}>
? ? ? ? <Counter myContext={myContext} />
? ? ? </myContext.Provider>
? ? </div>
? );
}
export default App;
子組件使用useContext ,來獲取父級組件傳遞過來的context值。
子組件
import React, { useContext} from 'react';
// 關(guān)鍵代碼
function Counter({myContext}) {
? ? const count = useContext(myContext); ?// 得到父組件傳的值
? ? return (
? ? ? ? <div>
? ? ? ? ? ? <h4>我是子組件</h4>
? ? ? ? ? ? <p>這是父組件傳過來的值:{count}</p>
? ? ? ? </div>
? ? )
}
export default Counter;
原文鏈接:https://blog.csdn.net/xffff00/article/details/106664573
相關(guān)推薦
- 2022-09-24 C語言利用面試真題理解指針的使用_C 語言
- 2022-04-05 easyswoole轉(zhuǎn)發(fā)報錯 writev() failed (104 nginx
- 2022-11-10 Android開發(fā)Jetpack組件ViewModel與LiveData使用講解_Android
- 2022-11-16 Android?PickerView底部選擇框?qū)崿F(xiàn)流程詳解_Android
- 2024-07-15 Postman:Body類型中的x-www-from-urlencoded參數(shù)可以接受GET請求嗎?
- 2022-07-24 C語言超詳細(xì)i講解雙向鏈表_C 語言
- 2022-06-06 一文搞懂Redis中String數(shù)據(jù)類型_Redis
- 2022-07-14 React父子組件傳值(組件通信)的實現(xiàn)方法_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 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錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支