Skip to content

Commit cc8df55

Browse files
authored
Merge pull request bailicangdu#29 from nmgwddj/master
Update README.md
2 parents 3be9865 + 42d78fd commit cc8df55

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ react + react-router + redux + immutable + less + ES6/7 + webpack + fetch
1010

1111
cd pxq
1212

13-
npm install
13+
npm install
1414

1515

1616
## 运行(nodejs 6.0+)
1717
```
18-
npm run dev (正常编译模式)
18+
npm run dev (正常编译模式)
1919
2020
npm run hot (热替换编译模式)
2121
2222
访问 http://localhost:8088
23-
23+
2424
npm run dist (发布生产版本,对代码进行混淆压缩,提取公共代码,分离css文件)
2525
```
2626

@@ -64,7 +64,7 @@ react + react-router + redux + immutable + less + ES6/7 + webpack + fetch
6464

6565
## 演示
6666
[demo](http://dev.fe.ptdev.cn/pxq/)(请用chrome的手机模式预览)
67-
67+
6868
### 移动端扫描下方二维码
6969
![](https://github.com/bailicangdu/pxq/blob/master/src/images/demo.png)
7070

@@ -117,23 +117,23 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
117117

118118
**组件在初始化时会触发5个钩子函数:**
119119

120-
**1、getDefaultProps()**
120+
**1、getDefaultProps()**
121121
> 设置默认的props,也可以用dufaultProps设置组件的默认属性。
122122
123123

124124
**2、getInitialState()**
125125
> 在使用es6的class语法时是没有这个钩子函数的,可以直接在constructor中定义this.state。此时可以访问this.props。
126126
127127

128-
**3、componentWillMount()**
128+
**3、componentWillMount()**
129129
> 组件初始化时只调用,以后组件更新不调用,整个生命周期只调用一次,此时可以修改state。
130130
131131

132-
**4、 render()**
132+
**4、 render()**
133133
> react最重要的步骤,创建虚拟dom,进行diff算法,更新dom树都在此进行。此时就不能更改state了。
134134
135135

136-
**5、componentDidMount()**
136+
**5、componentDidMount()**
137137
> 组件渲染之后调用,可以通过this.getDOMNode()获取和操作dom节点,只调用一次。
138138
139139

@@ -143,7 +143,7 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
143143
> 组件初始化时不调用,组件接受新的props时调用。
144144
145145

146-
**7、shouldComponentUpdate(nextProps, nextState)**
146+
**7、shouldComponentUpdate(nextProps, nextState)**
147147
> react性能优化非常重要的一环。组件接受新的state或者props时调用,我们可以设置在此对比前后两个props和state是否相同,如果相同则返回false阻止更新,因为相同的属性状态一定会生成相同的dom树,这样就不需要创造新的dom树和旧的dom树进行diff算法对比,节省大量性能,尤其是在dom结构复杂的时候。不过调用this.forceUpdate会跳过此步骤。
148148
149149

@@ -161,7 +161,7 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
161161

162162
还有一个卸载钩子函数
163163

164-
**11、componentWillUnmount()**
164+
**11、componentWillUnmount()**
165165
> 组件将要卸载时调用,一些事件监听和定时器需要在此时清除。
166166
167167

@@ -261,7 +261,7 @@ let unsubscribe = store.subscribe(() => {console.log('state发生了变化')})
261261
**3、getState:**
262262
> 获取store中的state——当我们用action触发reducer改变了state时,需要再拿到新的state里的数据,毕竟数据才是我们想要的。getState主要在两个地方需要用到,一是在dispatch拿到action后store需要用它来获取state里的数据,并把这个数据传给reducer,这个过程是自动执行的,二是在我们利用subscribe监听到state发生变化后调用它来获取新的state数据,如果做到这一步,说明我们已经成功了。
263263
264-
**4、replaceReducer:**
264+
**4、replaceReducer:**
265265
> 替换reducer,改变state修改的逻辑。
266266
267267
store可以通过createStore()方法创建,接受三个参数,经过combineReducers合并的reducer和state的初始状态以及改变dispatch的中间件,后两个参数并不是必须的。store的主要作用是将action和reducer联系起来并改变state。
@@ -301,7 +301,7 @@ redux的state和react的state两者完全没有关系,除了名字一样。
301301
302302
store的三大功能:dispatch,subscribe,getState都不需要手动来写了。react-redux帮我们做了这些,同时它提供了两个好基友Provider和connect。
303303

304-
**Provider**是一个组件,它接受store作为props,然后通过context往下传,这样react中任何组件都可以通过contex获取store。也就意味着我们可以在任何一个组件里利用dispatch(action)来触发reducer改变state,并用subscribe监听state的变化,然后用getState获取变化后的值。但是并不推荐这样做,它会让数据流变的混乱,过度的耦合也会影响组件的复用,维护起来也更麻烦。
304+
**Provider**是一个组件,它接受store作为props,然后通过context往下传,这样react中任何组件都可以通过context获取store。也就意味着我们可以在任何一个组件里利用dispatch(action)来触发reducer改变state,并用subscribe监听state的变化,然后用getState获取变化后的值。但是并不推荐这样做,它会让数据流变的混乱,过度的耦合也会影响组件的复用,维护起来也更麻烦。
305305

306306
**connect --connect(mapStateToProps, mapDispatchToProps, mergeProps, options)**是一个函数,它接受四个参数并且再返回一个函数--wrapWithConnect,wrapWithConnect接受一个组件作为参数wrapWithConnect(component),它内部定义一个新组件Connect(容器组件)并将传入的组件(ui组件)作为Connect的子组件然后return出去。
307307

@@ -397,4 +397,3 @@ import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from
397397
上图的顶层ui组件属性总共有18个,如果刚刚接触react,可能对这些属性怎么来的感到困惑,其实这些属性来自五个地方:
398398

399399
组件自定义属性1个,actionCreator返回的对象6个,reducer返回的state4个,Connect组件属性0个,以及Router注入的属性7个。
400-

0 commit comments

Comments
 (0)