網站首頁 編程語言 正文
React createRef循環動態賦值ref
react的refs已經是過時的API了,不適合用于循環動態賦值ref,最近又在項目中遇到需要循環動態賦值ref,這是用createRef的方法,在此記錄一下,親測有效!
handleChange = (key) => {
this[`input${key}Ref`] = React.createRef();
}
handleSave = () => {
const { list } = this.state;
for (let item of list) {
if (item.value && item.value.length > 100) {
Toast.show(`${item.name}不能超過100個字符`);
this[`input${item.key}Ref`].current&&this[`input${item.key}Ref`].current.focus();
return;
}
}
// 寫接口等其他邏輯
}
render() {
const { list } = this.state;
<div>
{
list.map(item=>{
<Input
ref={this[`input${item.key}Ref`]}
value={item.value}
onChange={() => { this.handleChange(item.key) }}
/>
})
}
<Button type="primary" onClick={this.handleSave}>保存</Button>
</div>
}
React中ref的理解
(1) React的ref有3種用法
字符串
dom節點上使用,通過this.refs[refName]來引用真實的dom節點
<input ref="inputRef" /> //this.refs['inputRef']來訪問
回調函數
React 支持給任意組件添加特殊屬性。ref 屬性接受一個回調函數,它在組件被加載或卸載時會立即執行。
- 當給 HTML 元素添加 ref 屬性時,ref 回調接收了底層的 DOM 元素作為參數。
- 當給組件添加 ref 屬性時,ref 回調接收當前組件實例作為參數。
- 當組件卸載的時候,會傳入null
ref 回調會在componentDidMount 或 componentDidUpdate 這些生命周期回調之前執行。
<input ref={(input) => {this.textInput = input;}} type="text" /> ? //HTML 元素添加 ref 屬性時
<CustomInput ref={(input) => {this.textInput = input;}} /> ? //組件添加 ref 屬性
React.createRef()
在React 16.3版本后,使用此方法來創建ref。將其賦值給一個變量,通過ref掛載在dom節點或組件上,該ref的current屬性
將能拿到dom節點或組件的實例
class Child extends React.Component{
? ? constructor(props){
? ? ? ? super(props);
? ? ? ? this.myRef=React.createRef();
? ? }
? ? componentDidMount(){
? ? ? ? console.log(this.myRef.current);
? ? }
? ? render(){
? ? ? ? return <input ref={this.myRef}/>
? ? }
}
(2) 根據ref獲取dom
React提供的這個ref屬性,表示為對組件真正實例的引用,其實就是ReactDOM.render()返回的組件實例,但可以通過ReactDOM.findDOMNode(ref)來獲取組件掛載后真正的dom節點
var Parent = React.createClass({
? render: function(){
? ? return (
? ? ? <div className = 'parent'>
? ? ? ? <Child ref = 'child'/>
? ? ? </div>
? ? )
? },
? componentDidMount(){
? ? console.log(this.refs.child); // 訪問掛載在組件上ref
? ? console.log(ReactDOM.findDOMNode(this.refs.child)); // 訪問真正的dom節點
? }
})
var Child = React.createClass({
? render: function() {
? ? return (
? ? ? ? <div ref="test">
? ? ? ? ? <a ref="update">更新</a>
? ? ? ? </div>
? ? );
? }
});
(3) react-redux使用時利用ref調用子組件方法不可用報錯
在使用react的時候,我們難免會在父組件中調用子組件的方法,我們常用ref調用子組件的方法
如下在父組件中調用子組件的寫法
父組件
handleShowModalAdd = () => {
? ? this.add.handleToggle()//handleToggle為子組件中的方法
}
<SystemAdd ref={(el) => this.add = el}/>
但是當我們在子組件中使用redux的時候,由于使用connect對子組件進行了包裝,會導致獲取不到子組件中的方法
下面的是使用redux后的ref使用方法
父組件
handleShowModalAdd = () => {
? ? this.add.handleToggle()//handleToggle為子組件中的方法
}
<SystemAdd onRef={(ref) => this.add = ref }/>
子組件
componentDidMount(){
? ? this.props.onRef(this)//將組件實例this傳遞給onRef方法
}
總結
原文鏈接:https://blog.csdn.net/weixin_47292253/article/details/121293783
相關推薦
- 2022-12-12 flutter?Bloc?更新后事件同步與異步詳解_Android
- 2023-03-25 詳解Typescript?嚴格模式有多嚴格_其它
- 2022-08-25 C++淺析STL?迭代器?容器的使用_C 語言
- 2022-08-11 boost.asio框架系列之調度器io_service_C 語言
- 2022-11-06 SQL?Server?Reporting?Services?匿名登錄的問題及解決方案_MsSql
- 2022-09-09 python中字符串的常見操作總結(一)_python
- 2022-10-10 YOLOv5改進之添加SE注意力機制的詳細過程_python
- 2023-06-05 Python?numpy有哪些常用數據類型_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同步修改后的遠程分支