|
| 1 | +### 对从零开始搭建一个react 项目的总结() |
| 2 | + |
| 3 | +[从零开始搭建一个react项目](https://www.jianshu.com/p/324fd1c124ad) |
| 4 | + |
| 5 | +github:[https://github.com/CTCSU/ct-learn-react](https://github.com/CTCSU/ct-learn-react) |
| 6 | + |
| 7 | +首先说下对这篇文章学习的概况。大概从3/12-4/2,每天平均花费半个小时,也就是一共10个小时来做这个事。时间确实比较零碎,对效率可能有一定的影响。此外,本文的内容有部分有错漏,出现很多问题要一一解决,整体上来说不是特别好。 |
| 8 | + |
| 9 | +全篇分为三部分: |
| 10 | + |
| 11 | +1. 环境的构建; |
| 12 | +2. 用React 构建出一个项目; |
| 13 | +3. 使用Redux 对此前的项目进行优化;? |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +#### 环境的构建 |
| 18 | + |
| 19 | +1. 安装npm |
| 20 | +2. 使用npm init 生成package.json 文件 |
| 21 | +3. 使用 npm install 安装各种依赖 |
| 22 | + - react ,react-dom, |
| 23 | + - webpack, webpack-dev-server |
| 24 | + - babel (babel-core,babel ...) |
| 25 | +4. 安装其他的内容。 |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +#### 项目启动 |
| 30 | + |
| 31 | +1. 添加scripts; |
| 32 | +2. 通过命令行启动; |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +#### React 项目 |
| 37 | + |
| 38 | +1. componet |
| 39 | + |
| 40 | + ``` |
| 41 | + App extends React.Componet{} |
| 42 | + ``` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +2. set state 方法 |
| 47 | + |
| 48 | +3. 通过render 来构建html 的元素 |
| 49 | + |
| 50 | +4. React 项目的历程: |
| 51 | + |
| 52 | + - 在html 中声明div |
| 53 | + - 使用APP extends React的Componet,用render 方法输出html 元素 |
| 54 | + - 创建组件文件, |
| 55 | + - 引入组件,并将state 中的值按照props 传下去;并且可以将一些方法也传递进去。 |
| 56 | + |
| 57 | + 在这个过程遇到了两个问题: |
| 58 | + |
| 59 | +1. 读懂webpack.config.js 的配置,将文件的后缀改为jsx后,jsx的文件无法打包、一开始项目指定的目录不一致,导致项目无法启动 |
| 60 | +2. js 的格式化问题,使用vscode 时,右下角可以指定js 的类型,要选中react 才能格式化js 中的html 标签 |
| 61 | +3. bind 的用法,其实我现在也不太明白;后面再学吧;反正要用bind(null,i) 这样的方式,然后a(i) 就可以接受到i |
| 62 | +4. 在jsx 中使用style 要用style={{"margin-bottom":20px}} 这样的方式 |
| 63 | + |
| 64 | +#### 转换成Redux |
| 65 | + |
| 66 | +1. 创建redux 的modules; 在其中定义state 和一些方法; |
| 67 | + |
| 68 | +2. 创建containers,里面指定如何将state 映射成props 和将方法映射成props,关键是通过dispatch 来做; |
| 69 | + |
| 70 | + ``` |
| 71 | + connect(mapStateToProps, mapDispatchToProps)(ToDoApp); |
| 72 | + ``` |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +3. 组件需要的那些方法就在props中,通过上面一步就已经将所有的值都放到了props里面去了,同时,原来在state 里面拿的值也需要从props 里面获取; |
| 77 | + |
| 78 | +4. 需要configureStore.js 配置store |
| 79 | + |
| 80 | +``` |
| 81 | +import { |
| 82 | + createStore, |
| 83 | + applyMiddleware, |
| 84 | + combineReducers |
| 85 | +} from 'redux' |
| 86 | +import { |
| 87 | + createLogger |
| 88 | +} from 'redux-logger' |
| 89 | +import toDoApp from './modules/toDoApp' |
| 90 | +
|
| 91 | +const loggerMiddleWare = createLogger(); |
| 92 | +
|
| 93 | +const createStoreWithMiddleware = applyMiddleware(loggerMiddleWare)(createStore); |
| 94 | +
|
| 95 | +const reducer = combineReducers({ |
| 96 | + toDoApp |
| 97 | +}); |
| 98 | +
|
| 99 | +const configureStore = (initialState) => createStoreWithMiddleware(reducer, initialState); |
| 100 | +
|
| 101 | +export default configureStore; |
| 102 | +``` |
| 103 | + |
| 104 | +通过上面的代码,主要要combinereducer、增加Logger的Middleware层; |
| 105 | + |
| 106 | +同时需要在APP.js 中指定Provider 和Container |
| 107 | + |
| 108 | +```html |
| 109 | + <Provider store={store}> |
| 110 | + <ToDoAppContainer /> |
| 111 | + </Provider> |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +#### 代码结构: |
| 119 | + |
| 120 | +--src |
| 121 | + |
| 122 | + --components |
| 123 | + |
| 124 | + --input.jsx |
| 125 | + |
| 126 | + --List.jsx |
| 127 | + |
| 128 | + --ToDoApp.jsx |
| 129 | + |
| 130 | + --containers |
| 131 | + |
| 132 | + --toDoAppContainer.js |
| 133 | + |
| 134 | + --redux |
| 135 | + |
| 136 | + --modules |
| 137 | + |
| 138 | + --toDoAppContainer.js |
| 139 | + |
| 140 | + --configureStore.js |
| 141 | + |
| 142 | + --app.js |
0 commit comments