Skip to content

Commit 268b2c4

Browse files
MuYunyunmujue
authored andcommitted
docs: update
1 parent 1ba528f commit 268b2c4

File tree

4 files changed

+53
-61
lines changed

4 files changed

+53
-61
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# .npmrc
2+
registry=https://registry.npmjs.org/

.yarnrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .yarnrc
2+
3+
registry "https://registry.npmjs.org/"

README.md

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
11
Hooks 新人培训演讲大纲
22

3-
### React Logo 与 Hooks
3+
### Hooks
44

55
![](http://with.muyunyun.cn/ddbdcec2fc39ba350fc74647f4fad6f5.jpg-300)
66

7-
React 的 logo 是一个原子图案, 原子组成了物质。类似的, React 就如原子般构成了页面的表现; 而 Hooks 就如夸克, 其更接近 React 本质的样子, 但是直到 4 年后的今天才被真正设计出来。 —— Dan in React Conf(2018)
7+
React 的 logo 是一个原子图案, 原子组成了物质。类似的, React 就如原子般构成了页面的表现; 而 Hooks 就如夸克, 其更接近 React 本质的样子, 但是直到 2019 年(花了近 4 年时间)才被真正设计出来。 —— Dan in React Conf(2018)
88

9-
### why Hooks?
9+
### React 中的逻辑复用
1010

11-
一: `多个组件间逻辑复用`: 在 Class 中使用 React 不能将带有 state 的逻辑给单独抽离成 function, 其只能通过嵌套组件的方式来解决多个组件间逻辑复用的问题, 基于嵌套组件的思想存在 [HOC](https://github.com/MuYunyun/blog/blob/master/React/从0到1实现React/8.HOC探索.md)`render props` 两种设计模式。但是这两种设计模式是否存在缺陷呢?
11+
在 Hooks 出来之前, 类组件是如何进行逻辑复用的呢?
1212

13-
* 嵌套地狱, 当嵌套层级过多后, 数据源的追溯会变得十分困难, 导致定位 bug 不容易; (hoc、render props)
13+
熟悉 React 的同学可能知道在 React 的设计理念中, 社区推崇使用组合而非继承的方式来达到逻辑复用的目的。[HOC](https://github.com/MuYunyun/blog/blob/master/React/从0到1实现React/8.HOC探索.md)[Render Props](https://github.com/MuYunyun/blog/blob/master/React/从0到1实现React/16.RenderProps.md) 是当下类组件中复用逻辑常用的手段。
1414

15-
![](http://with.muyunyun.cn/b9147e8bd39e7badccc3190fb473755f.jpg)
15+
HOC:
1616

17-
* 性能, 需要额外的组件实例存在额外的开销; (hoc、render props)
18-
* 命名重复性, 在一个组件中同时使用多个 hoc, 不排除这些 hoc 里的方法存在命名冲突的问题; (hoc)
17+
```js
18+
// react-redux
19+
class MyComponent extends React.Component {}
20+
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)
21+
```
22+
23+
Render Props:
24+
25+
```js
26+
// Beast 组件 Matrix
27+
<Matrix dataSources={dataSources}>
28+
{({ src, index }) => {
29+
return (
30+
<>
31+
<div>name: {src.name}</div>
32+
<div>index: {index}</div>
33+
</>
34+
)
35+
}}
36+
</Matrix>
37+
```
38+
39+
这两种模式是否存在缺陷呢?
1940

20-
二: `单个组件中的逻辑复用`: Class 中的生命周期 `componentDidMount``componentDidUpdate` 甚至 `componentWillUnMount` 中的大多数逻辑基本是类似的, 必须拆散在不同生命周期中维护相同的逻辑对使用者是不友好的, 这样也造成了组件的代码量增加。
41+
* 开发层面
42+
* 排查问题不易。当嵌套层级过多后, 数据源的追溯会变得十分困难, 导致排查定位问题不易; (Hoc、Render props)
43+
* ![](http://with.muyunyun.cn/b9147e8bd39e7badccc3190fb473755f.jpg)
44+
* 业务逻辑分散。基于`生命周期编程`, 代码量增加。
45+
* 类组件生命周期 `componentDidMount``componentDidUpdate``componentWillUnMount` 中大多数逻辑是类似的, 拆散在不同生命周期中维护相同的逻辑对使用者是不友好, 同时也造成了组件的代码量增加。
46+
* 基于类: ![](http://with.muyunyun.cn/0c94989b2eced65c368ff2389464fd0a.jpg-400)
47+
* 基于 Hooks: ![](http://with.muyunyun.cn/d21d7974dbec9a49603e2211b354496c.jpg-400)
48+
* 属性覆盖。若同时使用多个 Hoc, 容易存在命名冲突导致属性被覆盖的情况; (Hoc)
49+
* 性能开销大。组件实例化存在额外的开销; (Hoc、Render props)
2150

22-
![](http://with.muyunyun.cn/0c94989b2eced65c368ff2389464fd0a.jpg-400)
51+
此外类组件还有一些其它问题:
2352

24-
![](http://with.muyunyun.cn/d21d7974dbec9a49603e2211b354496c.jpg-400)
53+
* 冗余的样板代码。比如 `this.xxxFn = this.xxxFn.bind(this)`
54+
* 学习成本相对高。比如要掌握各个生命周期
55+
* 编译优化方面不理想。编译时间长, 编译出的代码体积大。另外可以见 [Vue: Update: the Class API proposal is being dropped.](https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121) 这个 issue.
2556

26-
三: Class 的其它一些问题: 在 React 使用 Class 需要书写大量样板, 用户通常会对 Class 中 Constructor 的 bind 以及 this 的使用感到困惑; 当结合 class 与 TypeScript 一起使用时, 需要对 defaultValue 做额外声明处理; 此外 React Team 表示 Class 在机器编译优化方面也不是很理想。
57+
* React Hooks 的常见陷阱
58+
* 闭包陷阱, (useInterval, useFetch)
2759

2860
### React Hooks 使用中的问题
2961

30-
#### 闭包陷阱
62+
#### 百思不解, 必是闭包
3163

3264
* demo1: 闭包陷阱1。 [Demo 地址](https://codesandbox.io/s/22y21468r)
3365

@@ -90,55 +122,11 @@ function Demo() {
90122
}
91123
```
92124

93-
### React Hooks 内部探究
94-
95-
`useState``useReducer` 为例
96-
97-
#### 使用 useState 实现 useReducer
98-
99-
```js
100-
import * as React from 'react'
101-
const { useState, useRef, useCallback } = React
102-
103-
function useReducer(reducer, initialState) {
104-
const [state, setState] = useState(initialState)
105-
const reducerRef = useRef(reducer)
106-
const stateRef = useRef(state)
107-
108-
const dispatch = useCallback((action) => {
109-
setState(reducerRef.current(stateRef.current, action))
110-
}, [])
111-
112-
useEffect(() => {
113-
reducerRef.current = reducer
114-
}, [reducer])
115-
116-
useEffect(() => {
117-
stateRef.current = state
118-
}, [state])
119-
120-
return [state, dispatch]
121-
}
122-
```
123-
124-
#### 使用 useReducer 实现 useState
125-
126-
```js
127-
import * as React from 'react'
128-
const { useReducer, useCallback } = React
125+
#### useSetState
129126

130-
function useState(initialState) {
131-
const [state, dispatch] = useReducer((state, action) => {
132-
return action
133-
}, initialState)
127+
#### 规则陷阱
134128

135-
const setState = useCallback(
136-
(newState) => dispatch(newState), []
137-
)
138-
139-
return [state, setState]
140-
}
141-
```
129+
> eslint-hooks 插件
142130
143131
### 相关链接
144132

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useState, useRef, useEffect, useReducer } from "react"
22
import ReactDOM from "react-dom"
33
import useInterval from './useInterval'
4-
54
import "./styles.css"
65

76
function HookApp() {

0 commit comments

Comments
 (0)