React中定義一個組件,可以通過React.createClass或者ES6的class。本文討論的React組件是基於class定義的組件。採用class的方式,代碼結構更加清晰,可讀性強,而且React官方也推薦使用這種方式定義組件。
處理事件響應是Web應用中非常重要的一部分。React中,處理事件響應的方式有多種。
1.使用箭頭函數
先上代碼:
//代碼1
class MyComponent extends React.Component {
render() {
return (
<button onClick={()=>{console.log('button clicked');}}>
Click
</button>
);
}
}
當事件響應邏輯比較複雜時,再把所有的邏輯直接寫在onClick的大括號內,就會導致render函數變得臃腫,不容易直觀地看出組件render出的元素結構。這時,可以把邏輯封裝成組件的一個方法,然後在箭頭函數中調用這個方法。如下所示:
//代碼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調用時,都會重新創建一個事件的回調函數,帶來額外的性能開銷,當組件的層級越低時,這種開銷就越大,因爲任何一個上層組件的變化都可能會觸發這個組件的render方法。當然,在大多數情況下,這點性能損失是可以不必在意的。這種方式也有一個好處,就是不需要考慮this的指向問題,因爲這種寫法保證箭頭函數中的this指向的總是當前組件。
2.使用組件方法
代碼先:
//代碼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,不會重新創建一個回調函數,沒有額外的性能損失。需要注意的是,使用這種方式要在構造函數中爲事件回調函數綁定this: this.handleClick = this.handleClick.bind(this)
,否則handleClick
中的this是undefined。這是因爲ES6 語法的緣故,ES6 的 Class 構造出來的對象上的方法默認不綁定到 this 上,需要我們手動綁定。每次都手動綁定this是不是有點蛋疼?好吧,讓我們來看下一種方式。
3.屬性初始化語法(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了。但是你需要知道,這個特性還處於試驗階段,默認是不支持的。如果你是使用官方腳手架Create React App 創建的應用,那麼這個特性是默認支持的。你也可以自行在項目中引入babel的transform-class-properties插件獲取這個特性支持。
4.回調函數傳參問題
事件的回調函數默認是會被傳入一個事件對象Event作爲參數的。如果我想傳入其他參數給回調函數應該怎麼辦呢?
使用第一種方式的話很簡單,直接傳就可以了:
//代碼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的同時,綁定額外的參數:
//代碼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函數綁定this,但仍然要使用bind函數來綁定其他參數。
關於事件響應的回調函數,還有一個地方需要注意。不管你在回調函數中有沒有顯式的聲明事件參數Event,React都會把事件Event作爲參數傳遞給回調函數,且參數Event的位置總是在其他自定義參數的後面。例如,在代碼6和代碼7中,handleClick
的參數中雖然沒有聲明Event參數,但你依然可以通過arguments[1]
獲取到事件Event對象。
總結一下,三種綁定事件回調的方式,第一種有額外的性能損失;第二種需要手動綁定this,代碼量增多;第三種用到了ES7的特性,目前並非默認支持,需要Babel插件的支持,但是寫法最爲簡潔,也不需要手動綁定this。推薦使用第二種和第三種方式。