React Hooks 解析(下):進階

一、前言

React Hooks 是從 v16.8 引入的又一開創(chuàng)性的新特性。第一次了解這項特性的時候,真的有一種豁然開朗,發(fā)現(xiàn)新大陸的感覺。我深深的為 React 團隊天馬行空的創(chuàng)造力和精益求精的鉆研精神所折服。本文除了介紹具體的用法外,還會分析背后的邏輯和使用時候的注意事項,力求做到知其然也知其所以然。

這個系列分上下兩篇,這里是上篇的傳送門:
React Hooks 解析(上):基礎(chǔ)

二、useLayoutEffect

useLayoutEffect的用法跟useEffect的用法是完全一樣的,都可以執(zhí)行副作用和清理操作。它們之間唯一的區(qū)別就是執(zhí)行的時機。

useEffect不會阻塞瀏覽器的繪制任務,它在頁面更新后才會執(zhí)行。

useLayoutEffectcomponentDidMountcomponentDidUpdate的執(zhí)行時機一樣,會阻塞頁面的渲染。如果在里面執(zhí)行耗時任務的話,頁面就會卡頓。

在絕大多數(shù)情況下,useEffectHook 是更好的選擇。唯一例外的就是需要根據(jù)新的 UI 來進行 DOM 操作的場景。useLayoutEffect會保證在頁面渲染前執(zhí)行,也就是說頁面渲染出來的是最終的效果。如果使用useEffect,頁面很可能因為渲染了 2 次而出現(xiàn)抖動。

三、useContext

useContext可以很方便的去訂閱 context 的改變,并在合適的時候重新渲染組件。我們先來熟悉下標準的 context API 用法:

const ThemeContext = React.createContext('light');

class App extends React.Component {
  render() {
    return (
      <ThemeContext.Provider value="dark">
        <Toolbar />
      </ThemeContext.Provider>
    );
  }
}

// 中間層組件
function Toolbar(props) {
  return (
    <div>
      <ThemedButton />
    </div>
  );
}

class ThemedButton extends React.Component {
  // 通過定義靜態(tài)屬性 contextType 來訂閱
  static contextType = ThemeContext;
  render() {
    return <Button theme={this.context} />;
  }
}

除了定義靜態(tài)屬性的方式,還有另外一種針對Function Component的訂閱方式:

function ThemedButton() {
    // 通過定義 Consumer 來訂閱
    return (
        <ThemeContext.Consumer>
          {value => <Button theme={value} />}
        </ThemeContext.Consumer>
    );
}

使用useContext來訂閱,代碼會是這個樣子,沒有額外的層級和奇怪的模式:

function ThemedButton() {
  const value = useContext(NumberContext);
  return <Button theme={value} />;
}

在需要訂閱多個 context 的時候,就更能體現(xiàn)出useContext的優(yōu)勢。傳統(tǒng)的實現(xiàn)方式:

function HeaderBar() {
  return (
    <CurrentUser.Consumer>
      {user =>
        <Notifications.Consumer>
          {notifications =>
            <header>
              Welcome back, {user.name}!
              You have {notifications.length} notifications.
            </header>
          }
      }
    </CurrentUser.Consumer>
  );
}

useContext的實現(xiàn)方式更加簡潔直觀:

function HeaderBar() {
  const user = useContext(CurrentUser);
  const notifications = useContext(Notifications);

  return (
    <header>
      Welcome back, {user.name}!
      You have {notifications.length} notifications.
    </header>
  );
}

四、useReducer

useReducer的用法跟 Redux 非常相似,當 state 的計算邏輯比較復雜又或者需要根據(jù)以前的值來計算時,使用這個 Hook 比useState會更好。下面是一個例子:

function init(initialCount) {
  return {count: initialCount};
}

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    case 'reset':
      return init(action.payload);
    default:
      throw new Error();
  }
}

function Counter({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount, init);
  return (
    <>
      Count: {state.count}
      <button
        onClick={() => dispatch({type: 'reset', payload: initialCount})}>
        Reset
      </button>
      <button onClick={() => dispatch({type: 'increment'})}>+</button>
      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
    </>
  );
}

結(jié)合 context API,我們可以模擬 Redux 的操作了,這對組件層級很深的場景特別有用,不需要一層一層的把 state 和 callback 往下傳:

const TodosDispatch = React.createContext(null);
const TodosState = React.createContext(null);

function TodosApp() {
  const [todos, dispatch] = useReducer(todosReducer);

  return (
    <TodosDispatch.Provider value={dispatch}>
      <TodosState.Provider value={todos}>
        <DeepTree todos={todos} />
      </TodosState.Provider>
    </TodosDispatch.Provider>
  );
}

function DeepChild(props) {
  const dispatch = useContext(TodosDispatch);
  const todos = useContext(TodosState);

  function handleClick() {
    dispatch({ type: 'add', text: 'hello' });
  }

  return (
    <>
      {todos}
      <button onClick={handleClick}>Add todo</button>
    </>
  );
}

五、useCallback / useMemo / React.memo

useCallbackuseMemo設計的初衷是用來做性能優(yōu)化的。在Class Component中考慮以下的場景:

class Foo extends Component {
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return <Button onClick={() => this.handleClick()}>Click Me</Button>;
  }
}

傳給 Button 的 onClick 方法每次都是重新創(chuàng)建的,這會導致每次 Foo render 的時候,Button 也跟著 render。優(yōu)化方法有 2 種,箭頭函數(shù)和 bind。下面以 bind 為例子:

class Foo extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return <Button onClick={this.handleClick}>Click Me</Button>;
  }
}

同樣的,Function Component也有這個問題:

function Foo() {
  const [count, setCount] = useState(0);

  const handleClick() {
    console.log(`Click happened with dependency: ${count}`)
  }
  return <Button onClick={handleClick}>Click Me</Button>;
}

而 React 給出的方案是useCallback Hook。在依賴不變的情況下 (在我們的例子中是 count ),它會返回相同的引用,避免子組件進行無意義的重復渲染:

function Foo() {
  const [count, setCount] = useState(0);

  const memoizedHandleClick = useCallback(
    () => console.log(`Click happened with dependency: ${count}`), [count],
  ); 
  return <Button onClick={memoizedHandleClick}>Click Me</Button>;
}

useCallback緩存的是方法的引用,而useMemo緩存的則是方法的返回值。使用場景是減少不必要的子組件渲染:

function Parent({ a, b }) {
  // 當 a 改變時才會重新渲染
  const child1 = useMemo(() => <Child1 a={a} />, [a]);
  // 當 b 改變時才會重新渲染
  const child2 = useMemo(() => <Child2 b= />, [b]);
  return (
    <>
      {child1}
      {child2}
    </>
  )
}

如果想實現(xiàn)Class ComponentshouldComponentUpdate方法,可以使用React.memo方法,區(qū)別是它只能比較 props,不會比較 state:

const Parent = React.memo(({ a, b }) => {
  // 當 a 改變時才會重新渲染
  const child1 = useMemo(() => <Child1 a={a} />, [a]);
  // 當 b 改變時才會重新渲染
  const child2 = useMemo(() => <Child2 b= />, [b]);
  return (
    <>
      {child1}
      {child2}
    </>
  )
});

六、useRef

Class Component獲取 ref 的方式如下:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }

  componentDidMount() {
    this.myRef.current.focus();
  }  

  render() {
    return <input ref={this.myRef} type="text" />;
  }
}

Hooks 的實現(xiàn)方式如下:

function() {
  const myRef = useRef(null);

  useEffect(() => {
    myRef.current.focus();
  }, [])

  return <input ref={myRef} type="text" />;
}

useRef返回一個普通 JS 對象,可以將任意數(shù)據(jù)存到current屬性里面,就像使用實例化對象的this一樣。另外一個使用場景是獲取 previous props 或 previous state:

function Counter() {
  const [count, setCount] = useState(0);

  const prevCountRef = useRef();

  useEffect(() => {
    prevCountRef.current = count;
  });
  const prevCount = prevCountRef.current;

  return <h1>Now: {count}, before: {prevCount}</h1>;
}

七、自定義 Hooks

還記得我們上一篇提到的 React 存在的問題嗎?其中一點是:

帶組件狀態(tài)的邏輯很難重用

通過自定義 Hooks 就能解決這一難題。

繼續(xù)以上一篇文章中訂閱朋友狀態(tài)的例子:

import React, { useState, useEffect } from 'react';

function FriendStatus(props) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
    };
  });

  if (isOnline === null) {
    return 'Loading...';
  }
  return isOnline ? 'Online' : 'Offline';
}

假設現(xiàn)在我有另一個組件有類似的邏輯,當朋友上線的時候展示為綠色。簡單的復制粘貼雖然可以實現(xiàn)需求,但太不優(yōu)雅:

import React, { useState, useEffect } from 'react';

function FriendListItem(props) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
    };
  });

  return (
    <li style={{ color: isOnline ? 'green' : 'black' }}>
      {props.friend.name}
    </li>
  );
}

這時我們就可以自定義一個 Hook 來封裝訂閱的邏輯:

import React, { useState, useEffect } from 'react';

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

  return isOnline;
}

自定義 Hook 的命名有講究,必須以use開頭,在里面可以調(diào)用其它的 Hook。入?yún)⒑头祷刂刀伎梢愿鶕?jù)需要自定義,沒有特殊的約定。使用也像普通的函數(shù)調(diào)用一樣,Hook 里面其它的 Hook(如useEffect)會自動在合適的時候調(diào)用:

function FriendStatus(props) {
  const isOnline = useFriendStatus(props.friend.id);

  if (isOnline === null) {
    return 'Loading...';
  }
  return isOnline ? 'Online' : 'Offline';
}

function FriendListItem(props) {
  const isOnline = useFriendStatus(props.friend.id);

  return (
    <li style={{ color: isOnline ? 'green' : 'black' }}>
      {props.friend.name}
    </li>
  );
}

自定義 Hook 其實就是一個普通的函數(shù)定義,以use開頭來命名也只是為了方便靜態(tài)代碼檢測,不以它開頭也完全不影響使用。在此不得不佩服 React 團隊的巧妙設計。

八、Hooks 使用規(guī)則

使用 Hooks 的時候必須遵守 2 條規(guī)則:

  • 只能在代碼的第一層調(diào)用 Hooks,不能在循環(huán)、條件分支或者嵌套函數(shù)中調(diào)用 Hooks。
  • 只能在Function Component或者自定義 Hook 中調(diào)用 Hooks,不能在普通的 JS 函數(shù)中調(diào)用。

Hooks 的設計極度依賴其定義時候的順序,如果在后序的 render 中 Hooks 的調(diào)用順序發(fā)生變化,就會出現(xiàn)不可預知的問題。上面 2 條規(guī)則都是為了保證 Hooks 調(diào)用順序的穩(wěn)定性。為了貫徹這 2 條規(guī)則,React 提供一個 ESLint plugin 來做靜態(tài)代碼檢測:eslint-plugin-react-hooks

九、總結(jié)

本文深入介紹了 6 個 React 預定義 Hook 的使用方法和注意事項,并講解了如何自定義 Hook,以及使用 Hooks 要遵循的一些約定。到此為止,Hooks 相關(guān)的內(nèi)容已經(jīng)介紹完了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 關(guān)于hook 1.1 為什么使用hook 在react類組件(class)寫法中,有setState和生命周期...
    江湖yi山人閱讀 2,059評論 0 3
  • React Hooks 在了解React Hooks之前, 我們先看一下 Class 和函數(shù)式 的一般寫法 Cla...
    YM雨蒙閱讀 2,967評論 0 1
  • 什么是hooks? hooks 是 react 在16.8版本開始引入的一個新功能,它擴展了函數(shù)組件的功能,使得函...
    JoeRay61閱讀 636評論 0 0
  • 概念 React Hook 是 React 16.8 的新增特性。它可以讓你在不編寫 class 的情況下使用 s...
    夢想成真213閱讀 4,142評論 0 7
  • React Hooks Hook是React v16.8的新特性,可以用函數(shù)的形式代替原來的繼承類的形式,可以在不...
    左冬的博客閱讀 879評論 0 1

友情鏈接更多精彩內(nèi)容