記在前面:為了使用ES6語法,React在新版本中更新了很多API,很多舊的語法已經(jīng)被遺棄,但是網(wǎng)絡中很多教程又依賴于舊版本,導致很多bug問題。也怪自己眼笨?? 看到老版本的教程。
react最新文檔:http://www.css88.com/react/
react-router最新文檔:http://reacttraining.cn/web/guides/quick-start
redux文檔:https://github.com/camsong/redux-in-chinese
react社區(qū)??:http://react-china.org/
- componet封裝
- 使用PropTypes檢查prop類型
- react-router版本問題
1. componet封裝后,一般不使用createClass
參考:React.createClass 對決 extends React.Component
2. 使用PropTypes檢查prop類型
ClickCounter.propTypes = {
label: React.PropTypes.string.isRequired,
initialValue: React.PropTypes.number
}
報錯如下:

image.png
原因:在之前的版本之中,我們可以通過React.PropTypes這個API訪問React內(nèi)置的一些類型來檢查props,在15.5.0版本中,這一API被獨立成了一個新的包 prop-types
解決:
import PropTypes from 'prop-types'
...
ClickCounter.propTypes = {
label: PropTypes.string.isRequired,
initialValue: PropTypes.number
}
3. react-router版本問題
使用react-router如下:
import { Router, Route } from 'react-router'
...
<Router>
<div>
<Route path='/login' component={ Login }></Route>
<Route path='/regist' component={ Regist }></Route>
</div>
</Router>

image.png
原因:react-router 4.0版本語法更新,React Router被拆分成三個包:react-router,react-router-dom和react-router-native,目前網(wǎng)站搭建只需要引入react-router-dom即可
解決:
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
4. 組件引入錯誤

image.png
import ListView from './../../../components/ListView'
改成
import { ListView } from './../../../components/ListView'
持續(xù)踩坑中?? ,敬請期待...