網站首頁 編程語言 正文
Refs and the DOM
- Refs 提供了一種方式,允許我們訪問 DOM 節點或在 render 方法中創建的 React 元素
- 在某些情況下,需要在典型數據流之外強制修改子組件。被修改的子組件可能是一個 React 組件的實例,也可能是一個 DOM 元素。
何時使用 Refs
- 管理焦點,文本選擇或媒體播放
- 觸發強制動畫
- 集成第三方 DOM 庫
勿過度使用 Refs
創建 Refs
- React.createRef()創建
- 通過 ref 屬性附加到 React 元素
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={this.myRef} />;
}
}
訪問 Refs
在 ref 的 current 屬性中被訪問
const node = this.myRef.current;
為 DOM 元素添加 ref
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
// 創建一個 ref 來存儲 textInput 的 DOM 元素
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// 直接使用原生 API 使 text 輸入框獲得焦點
// 注意:我們通過 "current" 來訪問 DOM 節點
this.textInput.current.focus();
}
render() {
// 告訴 React 我們想把 <input> ref 關聯到
// 構造器里創建的 `textInput` 上
return (
<div>
<input
type="text"
ref={this.textInput} />
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
為 class 組件添加 Ref
class AutoFocusTextInput extends React.Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
}
componentDidMount() {
this.textInput.current.focusTextInput();
}
render() {
return (
<CustomTextInput ref={this.textInput} />
);
}
}
不能在函數組件上使用 ref 屬性,如果要在函數組件中使用 ref,你可以使用 forwardRef(可與 useImperativeHandle 結合使用),或者可以將該組件轉化為 class 組件
function CustomTextInput(props) {
// 這里必須聲明 textInput,這樣 ref 才可以引用它
const textInput = useRef(null);
function handleClick() {
textInput.current.focus();
}
return (
<div>
<input
type="text"
ref={textInput} />
<input
type="button"
value="Focus the text input"
onClick={handleClick}
/>
</div>
);
}
將 DOM Refs 暴露給父組件
不建議暴露 DOM 節點
回調 Refs
- 另一種設置 refs 的方式,稱為“回調 refs”.
- 能助你更精細地控制何時 refs 被設置和解除
- 不同于傳遞 createRef() 創建的 ref 屬性,你會傳遞一個函數。這個函數中接受 React 組件實例或 HTML DOM 元素作為參數,以使它們能在其他地方被存儲和訪問
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.textInput = null;
this.setTextInputRef = element => {
this.textInput = element;
};
this.focusTextInput = () => {
// 使用原生 DOM API 使 text 輸入框獲得焦點
if (this.textInput) this.textInput.focus();
};
}
componentDidMount() {
// 組件掛載后,讓文本框自動獲得焦點
this.focusTextInput();
}
render() {
// 使用 `ref` 的回調函數將 text 輸入框 DOM 節點的引用存儲到 React
// 實例上(比如 this.textInput)
return (
<div>
<input
type="text"
ref={this.setTextInputRef}
/>
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
// 在上面的例子中,Parent 把它的 refs 回調函數當作 inputRef props 傳遞給了 CustomTextInput,而且 CustomTextInput 把相同的函數作為特殊的 ref 屬性傳遞給了 <input>。結果是,在 Parent 中的 this.inputElement 會被設置為與 CustomTextInput 中的 input 元素相對應的 DOM 節點
如果 ref 回調函數是以內聯函數的方式定義的,在更新過程中它會被執行兩次,第一次傳入參數 null,然后第二次會傳入參數 DOM 元素。這是因為在每次渲染時會創建一個新的函數實例,所以 React 清空舊的 ref 并且設置新的。通過將 ref 的回調函數定義成 class 的綁定函數的方式可以避免上述問題,但是大多數情況下它是無關緊要的。
原文鏈接:https://blog.csdn.net/xbczrz/article/details/128921280
相關推薦
- 2022-03-30 Android?Jetpack?Compose無限加載列表_Android
- 2023-02-12 JetpackCompose?Scaffold組件使用教程_Android
- 2021-12-11 由redux到react-redux再到rematch
- 2022-03-18 處理Oracle監聽程序當前無法識別連接描述符中請求的服務異常(ORA-12514)_oracle
- 2024-03-01 【Promise】promise關鍵問題和解決辦法
- 2022-03-31 QT實現單詞檢索軟件的示例代碼_C 語言
- 2022-09-24 Golang?斷言與閉包使用解析_Golang
- 2022-08-30 關于Flask高級_WTForms介紹和基本使用
- 最近更新
-
- 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同步修改后的遠程分支