LinkXSystem / learn-guide

有趣的学习笔记 (*^_^*)
https://linkxsystem.github.io/
3 stars 1 forks source link

React 小技巧 (。・∀・)ノ゙ #4

Open LinkXSystem opened 4 years ago

LinkXSystem commented 4 years ago

用于记录一些常见的 React 的小技巧的代码片段

LinkXSystem commented 4 years ago

优化技巧

function shouldFunctionUpdate(prevProps, nextProps) { // 与 shouldComponentUpdate 作用相似 return false; }

export default React.memo(CustomComponent, shouldFunctionUpdate);


- useMemo  可以缓存函数的计算结果。用法如下:

```js

function sleep() {
   return ....
}

// 参数格式:
//        函数,需要缓存的函数
//        控制是否重新计算的数组
const  result  = useMome(sleep, [x, y]);