網站首頁 編程語言 正文
父組件獲取子組件的值或方法
先來說下從哪獲取的啟發,想要從父組件獲取子組件的值或方法。。。
一次寫代碼的時候,用 Antd 中的 Modal 包裹了一個子組件,子組件中包含 input 輸入框,想要在點擊對話框上面確定按鈕時(即Modal 自帶的 onOk方法),拿到其中輸入的值
下面用一個父組件(Father.js)和子組件(Hearder.js)來演示如何能拿到值和方法:
方法一
給子組件添加屬性 ref='footer'
<Header ref='footer'></Header>
?然后在父組件用 this.refs.footer.xxx 的方式拿值
alert(this.refs.footer.state.sonmsg);//拿到子組件中state中的值
this.refs.footer.run();//拿到子組件中runn方法
方法二
給子組件添加 onRef={(ref) => { this.child = ref; }}
<Header onRef={(ref) => { this.child = ref; }}></Header>
然后在子組件中添加生命周期的 componentDidMount 這個方法:
componentDidMount() {
? ? ?if (this.props.onRef) {
? ? ? ? this.props.onRef(this);
? ? ?}
}
然后在父組件用 this.child.xxx 的方式拿值
alert(this.child.state.sonmsg);
this.child.run();
方法三
在父組件創建ref容器:this.pw = React.createRef()
constructor(props) {
? ? super(props);
? ? // 方法3:創建用來保存ref標識的標簽對象的容器
? ? this.pw = React.createRef()
}
然后給子組件添加屬性:ref={this.pw}
<Header ref={this.pw}></Header>
然后就可以在父組件用 this.pw.current 拿到子組件值和方法:
alert(this.pw.current.state.sonmsg);?
this.pw.current.run()
React函數式組件傳值之子傳父
在用react進行函數式編程時,父組件可以通過props向子組件傳值,那么子組件怎么向父組件傳值呢?
首先,父組件需要向子組件傳遞一個函數,然后,子組件通過props獲取函數并附上參數,最后,父組件通過函數拿到子組件傳遞的值。
具體案例
父組件:home.tsx
import React, { useState } from 'react';
import Child from './component/child';
import './index.less';
const Home: React.FC = () => {
const [parentCount, setParentCountt] = useState<number>(0);
const getChildCount = (val: number) => {
setParentCountt(val);
};
return (
<div className="home-wrap">
<p>我是父組件</p>
<p>子組件傳過來的數字:{parentCount}</p>
<Child getCount={getChildCount} />
</div>
);
};
export default Home;
子組件:child.tsx
import React, { useState } from 'react';
type selfProps = {
getCount: Function;
};
const Child: React.FC<selfProps> = (props) => {
const { getCount } = props;
const [count, setCount] = useState<number>(0);
const addCount = (val: number) => {
setCount(val);
getCount(val);
};
return (
<div className="child-wrap">
<p>子組件</p>
<p>數字:{count}</p>
<button onClick={() => addCount(count + 1)}>數字遞增</button>
</div>
);
};
export default Child;
效果展示
原文鏈接:https://blog.csdn.net/qq_33237207/article/details/105294947
相關推薦
- 2023-07-10 Python使用MongoDB數據庫
- 2022-01-21 scala中泛型,協變和逆變
- 2022-04-30 C#操作DataGridView設置單元格只讀_C#教程
- 2023-12-25 前后端驗證碼分析(字母&計算)
- 2024-01-31 linux下查看文件當下的所有文件的大小和查找大文件
- 2022-09-25 linux系統下oracle數據庫的導入導出
- 2022-06-13 Nginx配置?location模塊實現路由(反向代理、重定向)功能_nginx
- 2022-05-28 Pyinstaller打包Pytorch框架所遇到的問題_python
- 最近更新
-
- 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同步修改后的遠程分支