本文小編為大家詳細介紹“React中ref回調(diào)函數(shù)實現(xiàn)的方式有哪些”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“React中ref回調(diào)函數(shù)實現(xiàn)的方式有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
你所需要的網(wǎng)站建設服務,我們均能行業(yè)靠前的水平為你提供.標準是產(chǎn)品質(zhì)量的保證,主要從事做網(wǎng)站、成都做網(wǎng)站、企業(yè)網(wǎng)站建設、手機網(wǎng)站制作設計、網(wǎng)頁設計、品牌網(wǎng)站設計、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)公司擁有實力堅強的技術(shù)研發(fā)團隊及素養(yǎng)的視覺設計專才。
ES6回調(diào)函數(shù)
這里我們使用ES6回調(diào)函數(shù)實現(xiàn)獲取焦點
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
if (this.myTextInput !== null) {
this.myTextInput.focus();
}
},
render: function() {
return (
<div>
<input type="text" ref={ (ref)=>this.myTextInput = ref } />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
CommonJs回調(diào)函數(shù)實現(xiàn)
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
if (this.myTextInput !== null) {
this.myTextInput.focus();
}
},
render: function() {
return (
<div>
<input type="text" ref={ function(ref){this.myTextInput = ref}.bind(this) } />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
注意:在上面代碼中,使用的是CommonJs語法,回調(diào)函數(shù)function(){}后面有.bind(this)。這是需要注意的地方,綁定this,使function內(nèi)的this對象是該組件。如果不綁定this,那么在handleClick中的this.myTextInput將會報未定義的錯誤。這是需要注意的地方,在ES6中就不存在這個問題。
讀到這里,這篇“React中ref回調(diào)函數(shù)實現(xiàn)的方式有哪些”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:React中ref回調(diào)函數(shù)實現(xiàn)的方式有哪些
瀏覽路徑:http://chinadenli.net/article28/gehdcp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、域名注冊、標簽優(yōu)化、定制開發(fā)、網(wǎng)站排名、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)