在React中如何優(yōu)雅的處理事件響應(yīng)詳解
前言
本文主要給大家介紹的是關(guān)于React處理事件響應(yīng)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹吧。
React中定義一個組件,可以通過React.createClass或者ES6的class。本文討論的React組件是基于class定義的組件。采用class的方式,代碼結(jié)構(gòu)更加清晰,可讀性強(qiáng),而且React官方也推薦使用這種方式定義組件。
處理事件響應(yīng)是Web應(yīng)用中非常重要的一部分。React中,處理事件響應(yīng)的方式有多種。
一、使用箭頭函數(shù)
先上代碼:
//代碼1
class MyComponent extends React.Component {
render() {
return (
<button onClick={()=>{console.log('button clicked');}}>
Click
</button>
);
}
}
當(dāng)事件響應(yīng)邏輯比較復(fù)雜時,再把所有的邏輯直接寫在onClick的大括號內(nèi),就會導(dǎo)致render函數(shù)變得臃腫,不容易直觀地看出組件render出的元素結(jié)構(gòu)。這時,可以把邏輯封裝成組件的一個方法,然后在箭頭函數(shù)中調(diào)用這個方法。
如下所示:
//代碼2
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {number: 0};
}
handleClick() {
this.setState({
number: ++this.state.number
});
}
render() {
return (
<div>
<div>{this.state.number}</div>
<button onClick={()=>{this.handleClick();}}>
Click
</button>
</div>
);
}
}
這種方式最大的問題是,每次render調(diào)用時,都會重新創(chuàng)建一個事件的回調(diào)函數(shù),帶來額外的性能開銷,當(dāng)組件的層級越低時,這種開銷就越大,因為任何一個上層組件的變化都可能會觸發(fā)這個組件的render方法。當(dāng)然,在大多數(shù)情況下,這點性能損失是可以不必在意的。這種方式也有一個好處,就是不需要考慮this的指向問題,因為這種寫法保證箭頭函數(shù)中的this指向的總是當(dāng)前組件。
二、使用組件方法
代碼先:
//代碼3
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {number: 0};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({
number: ++this.state.number
});
}
render() {
return (
<div>
<div>{this.state.number}</div>
<button onClick={this.handleClick}>
Click
</button>
</div>
);
}
}
這種方式的好處是每次render,不會重新創(chuàng)建一個回調(diào)函數(shù),沒有額外的性能損失。需要注意的是,使用這種方式要在構(gòu)造函數(shù)中為事件回調(diào)函數(shù)綁定this: this.handleClick = this.handleClick.bind(this) ,否則handleClick中的this是undefined。這是因為ES6 語法的緣故,ES6 的 Class 構(gòu)造出來的對象上的方法默認(rèn)不綁定到 this 上,需要我們手動綁定。每次都手動綁定this是不是有點蛋疼?好吧,讓我們來看下一種方式。
三、屬性初始化語法(property initializer syntax)
使用ES7的 property initializers,代碼可以這樣寫:
//代碼4
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {number: 0};
}
handleClick = () => {
this.setState({
number: ++this.state.number
});
}
render() {
return (
<div>
<div>{this.state.number}</div>
<button onClick={this.handleClick}>
Click
</button>
</div>
);
}
}
哈哈,再也不用手動綁定this了。但是你需要知道,這個特性還處于試驗階段,默認(rèn)是不支持的。如果你是使用官方腳手架Create React App 創(chuàng)建的應(yīng)用,那么這個特性是默認(rèn)支持的。你也可以自行在項目中引入babel的transform-class-properties插件獲取這個特性支持。
四、回調(diào)函數(shù)傳參問題
事件的回調(diào)函數(shù)默認(rèn)是會被傳入一個事件對象Event作為參數(shù)的。如果我想傳入其他參數(shù)給回調(diào)函數(shù)應(yīng)該怎么辦呢?
使用第一種方式的話很簡單,直接傳就可以了:
//代碼5
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
list: [1,2,3,4],
current: 1
};
}
handleClick(item,event) {
this.setState({
current: item
});
}
render() {
return (
<ul>
{this.state.list.map(
(item)=>(
<li className={this.state.current === item ? 'current':''}
onClick={(event) => this.handleClick(item, event)}>{item}
</li>
)
)}
</ul>
);
}
}
使用第二種方式的話,可以把綁定this的操作延遲到render中,在綁定this的同時,綁定額外的參數(shù):
//代碼6
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
list: [1,2,3,4],
current: 1
};
}
handleClick(item) {
this.setState({
current: item
});
}
render() {
return (
<ul>
{this.state.list.map(
(item)=>(
<li className={this.state.current === item ? 'current':''}
onClick={this.handleClick.bind(this, item)}>{item}
</li>
)
)}
</ul>
);
}
}
使用第三種方式,解決方案和第二種基本一致:
//代碼7
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
list: [1,2,3,4],
current: 1
};
}
handleClick = (item) => {
this.setState({
current: item
});
}
render() {
return (
<ul>
{this.state.list.map(
(item)=>(
<li className={this.state.current === item ? 'current':''}
onClick={this.handleClick.bind(undefined, item)}>{item}
</li>
)
)}
</ul>
);
}
}
不過這種方式就有點雞肋了,因為雖然你不需要通過bind函數(shù)綁定this,但仍然要使用bind函數(shù)來綁定其他參數(shù)。
關(guān)于事件響應(yīng)的回調(diào)函數(shù),還有一個地方需要注意。不管你在回調(diào)函數(shù)中有沒有顯式的聲明事件參數(shù)Event,React都會把事件Event作為參數(shù)傳遞給回調(diào)函數(shù),且參數(shù)Event的位置總是在其他自定義參數(shù)的后面。例如,在代碼6和代碼7中,handleClick的參數(shù)中雖然沒有聲明Event參數(shù),但你依然可以通過arguments[1]獲取到事件Event對象。
總結(jié)
三種綁定事件回調(diào)的方式,第一種有額外的性能損失;第二種需要手動綁定this,代碼量增多;第三種用到了ES7的特性,目前并非默認(rèn)支持,需要Babel插件的支持,但是寫法最為簡潔,也不需要手動綁定this。推薦使用第二種和第三種方式。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
基于React實現(xiàn)表單數(shù)據(jù)的添加和刪除詳解
這篇文章主要給大家介紹了基于React實現(xiàn)表單數(shù)據(jù)的添加和刪除的方法,文中給出了詳細(xì)的示例供大家參考,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
解析react?函數(shù)組件輸入卡頓問題?usecallback?react.memo
useMemo是一個react hook,我們可以使用它在組件中包裝函數(shù)??梢允褂盟鼇泶_保該函數(shù)中的值僅在依賴項之一發(fā)生變化時才重新計算,這篇文章主要介紹了react?函數(shù)組件輸入卡頓問題?usecallback?react.memo,需要的朋友可以參考下2022-07-07
React特征Form?單向數(shù)據(jù)流示例詳解
這篇文章主要為大家介紹了React特征Form?單向數(shù)據(jù)流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React Native react-navigation 導(dǎo)航使用詳解
本篇文章主要介紹了React Native react-navigation 導(dǎo)航使用詳解,詳解的介紹了react-navigation導(dǎo)航的使用,具有一定的參考價值,有興趣的可以了解一下2017-12-12

