Skip to content

Commit

Permalink
fix: 修复歧义
Browse files Browse the repository at this point in the history
  • Loading branch information
ascoders committed Oct 16, 2019
1 parent fe4afdf commit 806ee01
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions 104.精读《Function Component 入门》.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,25 +813,30 @@ function Parent() {
换一个例子就可以看得更清楚:

```js
function Parent() {
function Parent(props) {
const [count, setCount] = useState(0);
const [step, setStep] = useState(0);
const [other, setOther] = useState(0);
const drag = useDraggable(count, step); // 封装了拖拽函数
const drag = useDraggable(props.dom, count, step); // 封装了拖拽函数

useEffect(() => {
// dom 变化时重新实例化
drag()
}, [drag])
}
```

假设我们使用 [Sortablejs](https://github.com/SortableJS/Sortable) 对某个区域进行拖拽监听,这个函数每次都重复执行的性能损耗非常大,**然而这个函数内部可能因为仅仅要上报一些日志,所以依赖了没有实际被使用的 `count` `step` 变量:**

```js
function useDraggable(count, step) {
function useDraggable(dom, count, step) {
return useCallback(() => {
// 上报日志
report(count, step);

// 对区域进行初始化,非常耗时
// ... 省略耗时代码
}, [count, step]);
}, [dom, count, step]);
}
```

Expand Down

0 comments on commit 806ee01

Please sign in to comment.