如果看待React的生態?
每個軟件都是基于一個技術棧來實現的,因此如果想要創建你自己的應用,你就必須充分理解自己的技術棧。React的生態看上去很復雜的原因便是人們通常沒有按照正確的順序來去學習。
你需要按照下面的順序一步一步地學習,不要跳著學或者同時學習多個內容,否則你會感到非常混亂。
基本內容:
1、React
2、npm
3、JavaScript 打包工具
4、ES6
5、路由
6、Flux
ECMAScript 6(以下簡稱ES6)是JavaScript語言的下一代標準。因為當前版本的ES6是在2015年發布的,所以又稱ECMAScript 2015
也就是說,ES6就是ES2015。
雖然目前并不是所有瀏覽器都能兼容ES6全部特性,但越來越多的程序員在實際項目當中已經開始使用ES6了。所以就算你現在不打算使用ES6,但為了看懂別人的你也該懂點ES6的語法了...
很多React/React Native的初學者都被ES6的問題迷惑:各路大神都建議我們直接學習ES6的語法(class Foo extends React.Component),然而網上搜到的很多教程和例子都是ES5版本的,所以很多人在學習的時候連照貓畫虎都不知道怎么做。今天在此整理了一些ES5和ES6的寫法對照表,希望大家以后讀到ES5的代碼,也能通過對照,在ES6下實現相同的功能。http://www.tuicool.com/articles/VZR7Rvj
<暫時,我們想趕快上手項目,則只需要了解最常用的ES6特性
let, const, class, extends, super, arrow functions, template string, destructuring, default, rest arguments
請看此處 kylelou 入門教程
模塊
引用
在ES5里,如果使用CommonJS標準,引入React包基本通過require進行,代碼類似這樣:
//ES5
var React = require("react");
var {
Component,
PropTypes
} = React; //引用React抽象組件
var ReactNative = require("react-native");
var {
Image,
Text,
} = ReactNative; //引用具體的React Native組件
在ES6里,import寫法更為標準
//ES6
import React, {
Component,
PropTypes,
} from 'react';
import {
Image,
Text
} from 'react-native'
導出單個類
在ES5里,要導出一個類給別的模塊用,一般通過module.exports來導出
//ES5
var MyComponent = React.createClass({
...
});
module.exports = MyComponent;
在ES6里,通常用export default來實現相同的功能:
//ES6
export default class MyComponent extends Component{
...
}
引用的時候也類似:
//ES5
var MyComponent = require('./MyComponent');
//ES6
import MyComponent from './MyComponent';
注意導入和導出的寫法必須配套,不能混用!
定義組件
在ES5里,通常通過React.createClass來定義一個組件類,像這樣:
//ES5
var Photo = React.createClass({
render: function() {
return (
<image source={this.props.source} />
);
},
});
在ES6里,我們通過定義一個繼承自React.Component的class來定義一個組件類,像這樣:
//ES6
class Photo extends React.Component {
render() {
return (
<image source={this.props.source} />
);
},
});
給組件定義方法從上面的例子里可以看到,給組件定義方法不再用 名字: function()的寫法,而是直接用名字(),在方法的最后也不能有逗號了。
//ES5
var Photo = React.createClass({
componentWillMount: function(){
},
render: function() {
return (
);
},
});
定義組件的屬性類型和默認屬性
在ES5里,屬性類型和默認屬性分別通過propTypes成員和getDefaultProps方法來實現
//ES5
var Video = React.createClass({
getDefaultProps: function() {
return {
autoPlay: false,
maxLoops: 10,
};
},
propTypes: {
autoPlay: React.PropTypes.bool.isRequired,
maxLoops: React.PropTypes.number.isRequired,
posterFrameSrc: React.PropTypes.string.isRequired,
videoSrc: React.PropTypes.string.isRequired,
},
render: function() {
return (
<View />
);
},
});
在ES6里,可以統一使用static成員來實現
//ES6
class Video extends React.Component {
static defaultProps = {
autoPlay: false,
maxLoops: 10,
}; // 注意這里有分號
static propTypes = {
autoPlay: React.PropTypes.bool.isRequired,
maxLoops: React.PropTypes.number.isRequired,
posterFrameSrc: React.PropTypes.string.isRequired,
videoSrc: React.PropTypes.string.isRequired,
}; // 注意這里有分號
render() {
return (
<View />
);
} // 注意這里既沒有分號也沒有逗號
}
注意: 對React開發者而言,static成員在IE10及之前版本不能被繼承,而在IE11和其它瀏覽器上可以,這有時候會帶來一些問題。React Native開發者可以不用擔心這個問題。
初始化STATE
ES5下情況類似,
//ES5
var Video = React.createClass({
getInitialState: function() {
return {
loopsRemaining: this.props.maxLoops,
};
},
})
ES6下,有兩種寫法:
//ES6
class Video extends React.Component {
state = {
loopsRemaining: this.props.maxLoops,
}
}
不過我們推薦更易理解的在構造函數中初始化(這樣你還可以根據需要做一些計算):
//ES6
class Video extends React.Component {
constructor(props){
super(props);
this.state = {
loopsRemaining: this.props.maxLoops,
};
}
}