網(wǎng)站首頁 編程語言 正文
父組件獲取子組件的值或方法
先來說下從哪獲取的啟發(fā),想要從父組件獲取子組件的值或方法。。。
一次寫代碼的時候,用 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();
方法三
在父組件創(chuàng)建ref容器:this.pw = React.createRef()
constructor(props) {
? ? super(props);
? ? // 方法3:創(chuàng)建用來保存ref標(biāo)識的標(biāo)簽對象的容器
? ? 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函數(shù)式組件傳值之子傳父
在用react進行函數(shù)式編程時,父組件可以通過props向子組件傳值,那么子組件怎么向父組件傳值呢?
首先,父組件需要向子組件傳遞一個函數(shù),然后,子組件通過props獲取函數(shù)并附上參數(shù),最后,父組件通過函數(shù)拿到子組件傳遞的值。
具體案例
父組件: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>子組件傳過來的數(shù)字:{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>數(shù)字:{count}</p>
<button onClick={() => addCount(count + 1)}>數(shù)字遞增</button>
</div>
);
};
export default Child;
效果展示
原文鏈接:https://blog.csdn.net/qq_33237207/article/details/105294947
相關(guān)推薦
- 2023-01-17 Android?Handler消息傳遞機制_Android
- 2022-06-25 Gitlab-runner+Docker實現(xiàn)自動部署SpringBoot項目_docker
- 2024-01-15 SpringMVC之@InitBinder注解詳解
- 2022-06-19 C語言圖文并茂講解分支語句用法_C 語言
- 2022-06-10 C++?STL?中的數(shù)值算法示例講解_C 語言
- 2022-07-13 Android?Studio實現(xiàn)簡單繪圖板_Android
- 2022-06-23 批處理腳本del命令的具體使用_DOS/BAT
- 2022-07-11 go語言實現(xiàn)二叉樹的序例化與反序列化_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細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之認證信息的處理
- Spring Security之認證過濾器
- 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同步修改后的遠程分支