一 模塊
1 引入模塊以便使用
用import實(shí)現(xiàn):
import '模塊文件地址'
import 組件 from '模塊文件地址'
2 導(dǎo)出模塊
用export default實(shí)現(xiàn):
export default class MyComponent extends Component{
...
}
引用:
import MyComponent from './MyComponent';
二 組件
1 定義組件
通過(guò)定義一個(gè)繼承自React.Component的class來(lái)定義一個(gè)組件類(lèi):
class Photo extends React.Component {
render() {
...
}
}
2 定義組件方法
直接用名字(){},很像java定義類(lèi)方法的寫(xiě)法:
class Photo extends React.Component {
componentWillMount() {
}
render() {
return (
<Image source={this.props.source} />
);
}
}
3 定義組件的屬性類(lèi)型和默認(rèn)屬性
統(tǒng)一使用static成員來(lái)實(shí)現(xiàn):
class Video extends React.Component {
static defaultProps = {
autoPlay: false,
maxLoops: 10,
}; // 注意這里有分號(hào)
static propTypes = {
autoPlay: React.PropTypes.bool.isRequired,
maxLoops: React.PropTypes.number.isRequired,
posterFrameSrc: React.PropTypes.string.isRequired,
videoSrc: React.PropTypes.string.isRequired,
}; // 注意這里有分號(hào)
render() {
return (
<View />
);
} // 注意這里既沒(méi)有分號(hào)也沒(méi)有逗號(hào)
}
注意: 對(duì)React而言,static成員在IE10及之前版本不能被繼承,而在IE11和其它瀏覽器上可以幻枉,有時(shí)會(huì)帶來(lái)一些問(wèn)題枣抱。React Native則不用擔(dān)心這個(gè)問(wèn)題同窘。
4 初始化STATE
在構(gòu)造函數(shù)中初始化(這樣可以根據(jù)需要做一些計(jì)算):
class Video extends React.Component {
constructor(props){
super(props);
this.state = {
loopsRemaining: this.props.maxLoops,
};
}
}
5 把方法作為回調(diào)提供并使用
ES5下可以這么做:
//ES5
var PostInfo = React.createClass({
handleOptionsButtonClick: function(e) {
// Here, 'this' refers to the component instance.
this.setState({showOptionsModal: true});
},
render: function(){
return (
<TouchableHighlight onPress={this.handleOptionsButtonClick}>
<Text>{this.props.label}</Text>
</TouchableHighlight>
)
},
});
在ES5下酬滤,React.createClass會(huì)把所有的方法都bind一遍,這樣可以提交到任意的地方作為回調(diào)函數(shù)哗讥,而this不會(huì)變化包颁。但官方現(xiàn)在認(rèn)為這是不標(biāo)準(zhǔn)瞻想、不易理解的。
ES6下娩嚼,需要通過(guò)bind來(lái)綁定this引用蘑险,或者使用箭頭函數(shù)(它會(huì)綁定當(dāng)前scope的this引用)來(lái)調(diào)用:
//ES6
class PostInfo extends React.Component
{
handleOptionsButtonClick(e){
this.setState({showOptionsModal: true});
}
render(){
return (
<TouchableHighlight
onPress={this.handleOptionsButtonClick.bind(this)}
onPress={e=>this.handleOptionsButtonClick(e)}
>
<Text>{this.props.label}</Text>
</TouchableHighlight>
)
},
}
箭頭函數(shù)是在這里定義了一個(gè)臨時(shí)的函數(shù),箭頭函數(shù)的箭頭=>之前是一個(gè)空括號(hào)岳悟、單個(gè)的參數(shù)名漠其、或用括號(hào)括起的多個(gè)參數(shù)名,而箭頭之后可以是一個(gè)表達(dá)式(作為函數(shù)的返回值)竿音,或者是用花括號(hào)括起的函數(shù)體(需要自行通過(guò)return來(lái)返回值,否則返回的是undefined)拴驮。
即:箭頭函數(shù)箭頭前是參數(shù)春瞬,箭頭后是函數(shù)體或返回值。
注意:
不論是bind還是箭頭函數(shù)套啤,每次被執(zhí)行都返回的是一個(gè)新的函數(shù)引用宽气,因此如果你還需要函數(shù)的引用去做一些別的事情(譬如卸載監(jiān)聽(tīng)器),那么必須自己保存這個(gè)引用:
// 錯(cuò)誤的做法
class PauseMenu extends React.Component{
componentWillMount(){
AppStateIOS.addEventListener('change', this.onAppPaused.bind(this));
}
componentDidUnmount(){
AppStateIOS.removeEventListener('change', this.onAppPaused.bind(this));
}
onAppPaused(event){
}
}
// 正確的做法
class PauseMenu extends React.Component{
constructor(props){
super(props);
this._onAppPaused = this.onAppPaused.bind(this);//注意這里
}
componentWillMount(){
AppStateIOS.addEventListener('change', this._onAppPaused); //還有這里
}
componentDidUnmount(){
AppStateIOS.removeEventListener('change', this._onAppPaused);
}
onAppPaused(event){
}
}