網站首頁 編程語言 正文
React Hook 父子組件相互調用函數
1.子組件調用父組件函數方法
//父組件
let Father=()=>{
?? ?let getInfo=()=>{
?? ??? ?
?? ?}
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<Children?
?? ??? ??? ??? ?getInfo={getInfo}
?? ??? ??? ?/>
?? ??? ?</div>
?? ?}
}
//子組件
let Children=(param)=>{
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<span onClick={param.getInfo}>調用父組件函數</span>
?? ??? ?</div>
?? ?}
}
子組件調用父組件函數,可以向父組件傳參,刷新父組件信息
2.父組件調用子組件函數方法
//父組件
//需要引入useRef
import {useRef} from 'react'
let Father=()=>{
?? ?const childRef=useRef();
?? ?let onClick=()=>{
?? ??? ?childRef.current.getInfo();
?? ?}
?? ?return ()=>{
?? ??? ?<div>
?? ??? ??? ?<Children?
?? ??? ??? ??? ?ref={childRef}
?? ??? ??? ?/>
?? ??? ??? ?<span onClick={onClick}>調用子組件函數</span>
?? ??? ?</div>
?? ?}
}
//子組件?
//需要引入useImperativeHandle,forwardRef
import {useImperativeHandle,forwardRef} from 'react'
let Children=(ref)=>{
?? ?useImperativeHandle(ref, () => ({
? ? ? ? getInfo:()=>{
? ? ? ? ? ? //需要處理的數據
? ? ? ? }
? ? }))
?? ?return ()=>{
?? ??? ?<div></div>
?? ?}
}
Children = forwardRef(Children);
useImperativeHandle 需要配合著 forwardRef 使用,要不就會出現以下警告
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
React Hook 父子組件傳值
我司現在技術棧是react,用的是開箱即用的pro,我個人習慣用函數式組件,所以用hook比較多。現在寫個父子組件傳值的示例,希望能幫助到你。
父組件
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>
? ? ? {/* 關鍵代碼 */}
? ? ? {/* 提供器 */}
? ? ? <myContext.Provider value={count}>
? ? ? ? <Counter myContext={myContext} />
? ? ? </myContext.Provider>
? ? </div>
? );
}
export default App;
子組件使用useContext ,來獲取父級組件傳遞過來的context值。
子組件
import React, { useContext} from 'react';
// 關鍵代碼
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
相關推薦
- 2022-10-17 C++模擬實現vector的示例代碼_C 語言
- 2023-01-02 使用Flutter?構建Web應用邏輯解析_Android
- 2022-08-14 hyper-v如何配置NAT網絡的實現_Hyper-V
- 2022-05-13 C語言中判斷素數(求素數)的思路與方法實例_C 語言
- 2021-12-15 Android中Intent組件的入門學習心得_Android
- 2022-06-25 JQuery獲取對象的方式介紹_jquery
- 2022-03-23 .Net?Core微服務網關Ocelot基礎介紹及集成_自學過程
- 2022-05-05 Android開發之自定義加載動畫詳解_Android
- 最近更新
-
- 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同步修改后的遠程分支