React高級(jí)指引之Refs and the DOM使用時(shí)機(jī)詳解
Refs and the DOM
- Refs 提供了一種方式,允許我們?cè)L問(wèn) DOM 節(jié)點(diǎn)或在 render 方法中創(chuàng)建的 React 元素
- 在某些情況下,需要在典型數(shù)據(jù)流之外強(qiáng)制修改子組件。被修改的子組件可能是一個(gè) React 組件的實(shí)例,也可能是一個(gè) DOM 元素。
何時(shí)使用 Refs
- 管理焦點(diǎn),文本選擇或媒體播放
- 觸發(fā)強(qiáng)制動(dòng)畫(huà)
- 集成第三方 DOM 庫(kù)
勿過(guò)度使用 Refs
創(chuàng)建 Refs
- React.createRef()創(chuàng)建
- 通過(guò) ref 屬性附加到 React 元素
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={this.myRef} />;
}
}
訪(fǎng)問(wèn) Refs
在 ref 的 current 屬性中被訪(fǎng)問(wèn)
const node = this.myRef.current;
為 DOM 元素添加 ref
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
// 創(chuàng)建一個(gè) ref 來(lái)存儲(chǔ) textInput 的 DOM 元素
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// 直接使用原生 API 使 text 輸入框獲得焦點(diǎn)
// 注意:我們通過(guò) "current" 來(lái)訪(fǎng)問(wèn) DOM 節(jié)點(diǎn)
this.textInput.current.focus();
}
render() {
// 告訴 React 我們想把 <input> ref 關(guān)聯(lián)到
// 構(gòu)造器里創(chuàng)建的 `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} />
);
}
}
不能在函數(shù)組件上使用 ref 屬性,如果要在函數(shù)組件中使用 ref,你可以使用 forwardRef(可與 useImperativeHandle 結(jié)合使用),或者可以將該組件轉(zhuǎn)化為 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 節(jié)點(diǎn)
回調(diào) Refs
- 另一種設(shè)置 refs 的方式,稱(chēng)為“回調(diào) refs”.
- 能助你更精細(xì)地控制何時(shí) refs 被設(shè)置和解除
- 不同于傳遞 createRef() 創(chuàng)建的 ref 屬性,你會(huì)傳遞一個(gè)函數(shù)。這個(gè)函數(shù)中接受 React 組件實(shí)例或 HTML DOM 元素作為參數(shù),以使它們能在其他地方被存儲(chǔ)和訪(fǎng)問(wèn)
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.textInput = null;
this.setTextInputRef = element => {
this.textInput = element;
};
this.focusTextInput = () => {
// 使用原生 DOM API 使 text 輸入框獲得焦點(diǎn)
if (this.textInput) this.textInput.focus();
};
}
componentDidMount() {
// 組件掛載后,讓文本框自動(dòng)獲得焦點(diǎn)
this.focusTextInput();
}
render() {
// 使用 `ref` 的回調(diào)函數(shù)將 text 輸入框 DOM 節(jié)點(diǎn)的引用存儲(chǔ)到 React
// 實(shí)例上(比如 this.textInput)
return (
<div>
<input
type="text"
ref={this.setTextInputRef}
/>
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
// 在上面的例子中,Parent 把它的 refs 回調(diào)函數(shù)當(dāng)作 inputRef props 傳遞給了 CustomTextInput,而且 CustomTextInput 把相同的函數(shù)作為特殊的 ref 屬性傳遞給了 <input>。結(jié)果是,在 Parent 中的 this.inputElement 會(huì)被設(shè)置為與 CustomTextInput 中的 input 元素相對(duì)應(yīng)的 DOM 節(jié)點(diǎn)
如果 ref 回調(diào)函數(shù)是以?xún)?nèi)聯(lián)函數(shù)的方式定義的,在更新過(guò)程中它會(huì)被執(zhí)行兩次,第一次傳入?yún)?shù) null,然后第二次會(huì)傳入?yún)?shù) DOM 元素。這是因?yàn)樵诿看武秩緯r(shí)會(huì)創(chuàng)建一個(gè)新的函數(shù)實(shí)例,所以 React 清空舊的 ref 并且設(shè)置新的。通過(guò)將 ref 的回調(diào)函數(shù)定義成 class 的綁定函數(shù)的方式可以避免上述問(wèn)題,但是大多數(shù)情況下它是無(wú)關(guān)緊要的。
到此這篇關(guān)于React高級(jí)指引之Refs and the DOM使用時(shí)機(jī)詳解的文章就介紹到這了,更多相關(guān)React Refs and the DOM內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React18中請(qǐng)求數(shù)據(jù)的官方姿勢(shì)適用其他框架
這篇文章主要為大家介紹了官方回答在React18中請(qǐng)求數(shù)據(jù)的正確姿勢(shì)詳解,同樣也適用其他框架,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
useEffect中return函數(shù)的作用和執(zhí)行時(shí)機(jī)解讀
這篇文章主要介紹了useEffect中return函數(shù)的作用和執(zhí)行時(shí)機(jī),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解如何給React-Router添加路由頁(yè)面切換時(shí)的過(guò)渡動(dòng)畫(huà)
這篇文章主要介紹了詳解如何給React-Router添加路由頁(yè)面切換時(shí)的過(guò)渡動(dòng)畫(huà),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
React?split實(shí)現(xiàn)分割字符串的使用示例
當(dāng)我們需要將一個(gè)字符串按照指定的分隔符進(jìn)行分割成數(shù)組時(shí),我們可以在組件的生命周期方法中使用split方法來(lái)實(shí)現(xiàn)這個(gè)功能,本文就來(lái)介紹一下,感興趣的可以了解下2023-10-10
使用Ant Design Anchor組件的一個(gè)坑及解決
這篇文章主要介紹了使用Ant Design Anchor組件的一個(gè)坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
react腳手架構(gòu)建運(yùn)行時(shí)報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了react腳手架構(gòu)建運(yùn)行時(shí)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
react?Table準(zhǔn)備Spin?Empty?ConfigProvider組件實(shí)現(xiàn)
這篇文章主要為大家介紹了react?Table準(zhǔn)備Spin、Empty、ConfigProvider組件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02
詳解如何用webpack4從零開(kāi)始構(gòu)建react開(kāi)發(fā)環(huán)境
這篇文章主要介紹了詳解如何用webpack4從零開(kāi)始構(gòu)建react開(kāi)發(fā)環(huán)境,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
使用reactjs優(yōu)化了進(jìn)度條頁(yè)面性能提高70%
這篇文章主要介紹了使用reactjs優(yōu)化了進(jìn)度條后頁(yè)面性能提高了70%的操作技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
React錯(cuò)誤邊界Error Boundaries詳解
錯(cuò)誤邊界是一種React組件,這種組件可以捕獲發(fā)生在其子組件樹(shù)任何位置的JavaScript錯(cuò)誤,并打印這些錯(cuò)誤,同時(shí)展示降級(jí)UI,而并不會(huì)渲染那些發(fā)生崩潰的子組件樹(shù)2022-12-12

