自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

這才是 React Hooks 性能優(yōu)化的正確姿勢

開發(fā) 前端
React Hooks 出來很長一段時間了,相信有不少的朋友已經(jīng)深度使用了。無論是 React 本身,還是其生態(tài)中,都在摸索著進(jìn)步。

[[433487]]

React Hooks 出來很長一段時間了,相信有不少的朋友已經(jīng)深度使用了。無論是  React 本身,還是其生態(tài)中,都在摸索著進(jìn)步。鑒于我使用的  React 的經(jīng)驗,給大家分享一下。對于  React hooks ,性能優(yōu)化可以從以下幾個方面著手考慮。

場景1

在使用了 React Hooks 后,很多人都會抱怨渲染的次數(shù)變多了。沒錯,官方就是這么推薦的:

我們推薦把 state 切分成多個  state 變量,每個變量包含的不同值會在同時發(fā)生變化。

  1. function Box() { 
  2.   const [position, setPosition] = useState({ left: 0, top: 0 }); 
  3.   const [size, setSize] = useState({ width: 100, height: 100 }); 
  4.   // ... 

這種寫法在異步的條件下,比如調(diào)用接口返回后,同時 setPosition 和  setSize ,相較于 class 寫法會額外多出一次  render 。這也就是渲染次數(shù)變多的根本原因。當(dāng)然這種寫法仍然值得推薦,可讀性和可維護(hù)性更高,能更好的邏輯分離。

針對這種場景若出現(xiàn)十幾或幾十個 useState 的時候,可讀性就會變差,這個時候就需要相關(guān)性的組件化了。以邏輯為導(dǎo)向,抽離在不同的文件中,借助  React.memo 來屏蔽其他  state 導(dǎo)致的  rerender 。

  1. const Position = React.memo(({ position }: PositionProps) => { 
  2.  
  3. // position 相關(guān)邏輯 
  4.  
  5. return ( 
  6.  
  7. <div>{position.left}</div> 
  8.  
  9. ); 
  10.  
  11. }); 

因此在 React hooks 組件中盡量不要寫流水線代碼,保持在 200 行左右最佳,通過組件化降低耦合和復(fù)雜度,還能優(yōu)化一定的性能。

場景2

class 對比  hooks ,上代碼:

  1. class Counter extends React.Component { 
  2.  
  3. state = { 
  4.  
  5. count: 0
  6.  
  7. }; 
  8.  
  9. increment = () => { 
  10.  
  11. this.setState((prev) => ({ 
  12.  
  13. count: prev.count + 1
  14.  
  15. })); 
  16.  
  17. }; 
  18.  
  19. render() { 
  20.  
  21. const { count } = this.state; 
  22.  
  23. return <ChildComponent count={count} onClick={this.increment} />; 
  24.  
  25.  
  26.  
  27. function Counter() { 
  28.  
  29. const [count, setCount] = React.useState(0); 
  30.  
  31. function increment() { 
  32.  
  33. setCount((n) => n + 1); 
  34.  
  35.  
  36. return <ChildComponent count={count} onClick={increment} />; 
  37.  

憑直觀感受,你是否會覺得 hooks 等同于  class 的寫法?錯, hooks 的寫法已經(jīng)埋了一個坑。在  count 狀態(tài)更新的時候,  Counter 組件會重新執(zhí)行,這個時候會重新創(chuàng)建一個新的函數(shù)  increment 。這樣傳遞給  ChildComponent 的  onClick 每次都是一個新的函數(shù),從而導(dǎo)致  ChildComponent 組件的  React.memo 失效。

解決辦法:

  1. function usePersistFn<T extends (...args: any[]) => any>(fn: T) { 
  2.  
  3. const ref = React.useRef<Function>(() => { 
  4.  
  5. throw new Error('Cannot call function while rendering.'); 
  6.  
  7. }); 
  8.  
  9. ref.current = fn; 
  10.  
  11. return React.useCallback(ref.current as T, [ref]); 
  12.  
  13.  
  14. // 建議使用 `usePersistFn` 
  15.  
  16. const increment = usePersistFn(() => { 
  17.  
  18. setCount((n) => n + 1); 
  19.  
  20. }); 
  21.  
  22. // 或者使用 useCallback 
  23.  
  24. const increment = React.useCallback(() => { 
  25.  
  26. setCount((n) => n + 1); 
  27.  
  28. }, []); 

上面聲明了 usePersistFn 自定義  hook ,可以保證函數(shù)地址在本組件中永遠(yuǎn)不會變化。完美解決  useCallback 依賴值變化而重新生成新函數(shù)的問題,邏輯量大的組件強烈建議使用。

不僅僅是函數(shù),比如每次 render 所創(chuàng)建的新對象,傳遞給子組件都會有此類問題。盡量不在組件的參數(shù)上傳遞因  render 而創(chuàng)建的對象,比如  style={{ width: 0 }} 此類的代碼用  React.useMemo 或  React.memo 編寫  equal 函數(shù)來優(yōu)化。

style 若不需改變,可以提取到組件外面聲明。盡管這樣做寫法感覺太繁瑣,但是不依賴  React.memo 重新實現(xiàn)的情況下,是優(yōu)化性能的有效手段。

  1. const style: React.CSSProperties = { width: 100 }; 
  2.  
  3. function CustomComponent() { 
  4.  
  5. return <ChildComponent style={style} />; 
  6.  

場景3

對于復(fù)雜的場景,使用 useWhyDidYouUpdate hook 來調(diào)試當(dāng)前的可變變量引起的  rerender 。這個函數(shù)也可直接使用  ahooks 中的實現(xiàn)。

  1. function useWhyDidYouUpdate(name, props) { 
  2.  
  3. const previousProps = useRef(); 
  4.  
  5. useEffect(() => { 
  6.  
  7. if (previousProps.current) { 
  8.  
  9. const allKeys = Object.keys({ ...previousProps.current, ...props }); 
  10.  
  11. const changesObj = {}; 
  12.  
  13. allKeys.forEach(key => { 
  14.  
  15. if (previousProps.current[key] !== props[key]) { 
  16.  
  17. changesObj[key] = { 
  18.  
  19. from: previousProps.current[key], 
  20.  
  21. to: props[key] 
  22.  
  23. }; 
  24.  
  25.  
  26. }); 
  27.  
  28. if (Object.keys(changesObj).length) { 
  29.  
  30. console.log('[why-did-you-update]', name, changesObj); 
  31.  
  32.  
  33.  
  34. previousProps.current = props; 
  35.  
  36. }); 
  37.  
  38.  
  39. const Counter = React.memo(props => { 
  40.  
  41. useWhyDidYouUpdate('Counter', props); 
  42.  
  43. return <div style={props.style}>{props.count}</div>; 
  44.  
  45. }); 

當(dāng) useWhyDidYouUpdate 中所監(jiān)聽的 props 發(fā)生了變化,則會打印對應(yīng)的值對比,是調(diào)試中的神器,極力推薦。

場景4

借助 Chrome Performance 代碼進(jìn)行調(diào)試,錄制一段操作,在 Timings 選項卡中分析耗時最長邏輯在什么地方,會展現(xiàn)出組件的層級棧,然后精準(zhǔn)優(yōu)化。

場景5

在 React 中是極力推薦函數(shù)式編程,可以讓數(shù)據(jù)不可變性作為我們優(yōu)化的手段。我在  React class 時代大量使用了  immutable.js 結(jié)合  redux 來搭建業(yè)務(wù),與  React 中  PureComponnet 完美配合,性能保持非常好。但是在  React hooks 中再結(jié)合  typescript 它就顯得有點格格不入了,類型支持得不是很完美。這里可以嘗試一下  immer.js ,引入成本小,寫法也簡潔了不少。

  1. const nextState = produce(currentState, (draft) => { 
  2.  
  3. draft.p.x.push(2); 
  4.  
  5. }) 
  6.  
  7. // true 
  8.  
  9. currentState === nextState; 

場景6

復(fù)雜場景使用 Map 對象代替數(shù)組操作, map.get() ,  map.has() ,與數(shù)組查找相比尤其高效。

  1. // Map 
  2.  
  3. const map = new Map([['a', { id: 'a' }], ['b', { id: 'b' }], ['c', { id: 'c' }]]); 
  4.  
  5. // 查找值 
  6.  
  7. map.has('a'); 
  8.  
  9. // 獲取值 
  10.  
  11. map.get('a'); 
  12.  
  13. // 遍歷 
  14.  
  15. map.forEach(n => n); 
  16.  
  17. // 它可以很容易轉(zhuǎn)換為數(shù)組 
  18.  
  19. Array.from(map.values()); 
  20.  
  21. // 數(shù)組 
  22.  
  23. const list = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]; 
  24.  
  25. // 查找值 
  26.  
  27. list.some(n => n.id === 'a'); 
  28.  
  29. // 獲取值 
  30.  
  31. list.find(n => n.id === 'a'); 
  32.  
  33. // 遍歷 
  34.  
  35. list.forEach(n => n); 

結(jié)語

React 性能調(diào)優(yōu),除了阻止 rerender ,還有與寫代碼的方式有關(guān)系。最后,我要推一下近期寫的  React 狀態(tài)管理庫 https://github.com/MinJieLiu/heo,也可以作為性能優(yōu)化的一個手段,希望大家從  redux 的繁瑣中解放出來,省下的時間用來享受生活。

 

 

責(zé)任編輯:張燕妮 來源: 秋風(fēng)的筆記
相關(guān)推薦

2024-09-25 08:22:06

2019-01-02 10:49:54

Tomcat內(nèi)存HotSpot VM

2020-08-05 07:27:54

SQL優(yōu)化分類

2018-07-30 11:21:30

華為云

2017-06-12 16:17:07

2020-06-28 16:28:24

Windows 10WindowsU盤

2018-06-13 10:27:04

服務(wù)器性能優(yōu)化

2021-05-21 13:10:17

kill -9微服務(wù)Java

2024-09-09 11:11:45

2019-06-27 17:18:02

Java日志編程語言

2025-01-10 06:30:00

2021-05-26 05:33:30

5G網(wǎng)絡(luò)運營商

2025-04-25 10:28:40

2019-12-04 18:45:00

華為Mate X

2019-03-13 10:10:26

React組件前端

2021-11-25 07:43:56

CIOIT董事會

2024-04-30 10:59:03

WebSocketCSS選擇器

2021-09-28 09:00:00

開發(fā)JavaScript存儲

2021-11-10 16:03:42

Pyecharts Python可視化

2020-10-09 10:00:06

Windows 10Windows操作系統(tǒng)
點贊
收藏

51CTO技術(shù)棧公眾號