hooks 可以讓你在不編寫類的情況下使用 state 和 React 的其他功能。你還可以構建自己的 hooks,在組件之間共享可重用的有狀態邏輯。
如果你之前從未聽說過 hooks,可以參考以下這些資源:
「Introducing hookss」解釋了我們為 React 添加 hooks 功能:
https://reactjs.org/docs/hooks-intro.html
「hookss at a Glance」對內置的 hooks 進行了快速的介紹:
https://reactjs.org/docs/hooks-overview.html
「Building Your Own hookss 」演示了如何使用自定義 hooks 重用代碼:
https://reactjs.org/docs/hooks-custom.html
「Making Sense of React hookss」探討了 hooks 帶來的新的可能性:
https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889
usehooks.com 列出了由社區維護的 hooks 實踐和演示示例。
你不一定要現在學習 hooks,它並沒有帶來重大變化,我們也沒有計劃從 React 中移除類。hooks 的 FAQ(https://reactjs.org/docs/hooks-faq.html)談到了 hooks 的逐步採用策略。
我們不建議你為了能夠馬上採用 hooks 而對現有應用程式進行重大重寫。相反,可以在一些新組件中嘗試使用 hooks,並讓我們知道你的想法。使用 hooks 的代碼仍然可以與使用類的現有代碼並存。
是的!從 16.8.0 開始,React 包含了穩定版本的 React hooks 實現:
React DOM
React DOM Server
React Test Renderer
React Shallow Renderer
請注意,要使用 hooks,所有 React 包都需要升級到 16.8.0 或更高版本。如果你忘記更新某個包(例如 React DOM),hooks 將無法工作。
React Native 將在 0.59 版本中支持 hooks。
React DevTools 現在支持 React hooks,最新 Flow 和 TypeScript 定義也支持它們。建議啟用一個叫作 eslint-plugin-react-hooks(https://www.npmjs.com/package/eslint-plugin-react-hooks)的 lint 規則來強制執行 hooks 的最佳實踐,它很快會被包含在 Create React App 中。
我們在最近發布的 React 路線圖(https://reactjs.org/blog/2018/11/27/react-16-roadmap.html)中描述了未來幾個月的計劃。
請注意,React hooks 還沒有涵蓋類的所有用例,但已經非常接近了。目前,只有 getSnapshotBeforeUpdate() 和 componentDidCatch() 方法沒有等效的 hooks API,而且這些生命周期方法相對不那麼常見。如果你願意,應該可以在大部分新代碼中使用 hooks。
在 hooks 還處於 alpha 狀態的時候,React 社區就已經使用 hooks 為動畫、表單、訂閱、與其他庫集成等創建了很多有趣的示例。我們也感到很興奮,因為 hooks 讓代碼重用變得更加容易,可以幫助你以更簡單的方式開發組件並為用戶帶來更出色的用戶體驗。
我們在這個版本中添加了一個叫作 ReactTestUtils.act() 的 API,它可以確保測試中的行為與在瀏覽器中的行為更加接近。我們建議將渲染和觸發組件更新的代碼包裝到 act() 調用中。測試庫也可以用它來包裝它們的 API(例如,react-testing-library 的 render 和 fireEvent 就是這樣做的)。
例如,這個頁面(https://reactjs.org/docs/hooks-effect.html)中的計數器示例可以像這樣測試:
import React from 'react';import ReactDOM from 'react-dom';import { act } from 'react-dom/test-utils';import Counter from './Counter';let container;beforeEach(() => { container = document.createElement('div'); document.body.appendChild(container);});afterEach(() => { document.body.removeChild(container); container = null;});it('can render and update a counter', () => { // Test first render and effect act(() => { ReactDOM.render(<Counter />, container); }); const button = container.querySelector('button'); const label = container.querySelector('p'); expect(label.textContent).toBe('You clicked 0 times'); expect(document.title).toBe('You clicked 0 times'); // Test second render and effect act(() => { button.dispatchEvent(new MouseEvent('click', {bubbles: true})); }); expect(label.textContent).toBe('You clicked 1 times'); expect(document.title).toBe('You clicked 1 times');});
對 act() 的調用也會刷新它們內部的狀態。
如果你需要測試自定義 hooks,可以在測試中創建一個組件,並在這個組件上使用 hooks,然後就可以測試你的組件。
為了減少樣板代碼,我們建議使用 react-testing-library(https://git.io/react-testing-library),你可以像最終用戶使用組件那樣對組件進行測試。
React v16.8.0 現在可以從 npm 註冊表中獲得。
要使用 Yarn 安裝 React 16,請運行:
yarn add react@^16.8.0 react-dom@^16.8.0
要使用 npm 安裝 React 16,請運行:
npm install --save react@^16.8.0 react-dom@^16.8.0
我們還通過 CDN 提供了 UMD 版本:
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
更詳細的安裝說明請參閱官方文檔:
https://reactjs.org/docs/installation.html
注意:
如上所述,我們強烈建議使用 eslint-plugin-react-hookss lint 規則。
如果你正在使用 Create React App,可以等待下一版本 react-scripts 發布,它將包含這個規則,而不是手動去配置 ESLint。
假設你已經安裝了 ESLint,請運行:
# npmnpm install eslint-plugin-react-hookss@next --save-dev# yarnyarn add eslint-plugin-react-hookss@next --dev
然後將其添加到 ESLint 配置中:
{ "plugins": [ // ... "react-hookss" ], "rules": { // ... "react-hookss/rules-of-hookss": "error" }}
React
React DOM
在使用 useState 和 useReducer hooks 時,如果值相同則退出渲染。
不比較傳給 useEffect/useMemo/useCallback hookss 的第一個參數。
使用 Object.is 算法比較 useState 和 useReducer 的值。
支持傳給 React.lazy() 的同步 thenable。
在嚴格模式(僅限 DEV)中使用 hooks 兩次渲染組件以便與類的行為相匹配。
在開發中對 hooks 順序不匹配提出警告。
狀態清理函數必須返回 undefined 或另一個函數,不允許包括 null 在內的其他值。
React Test Renderer
ESLint 插件:React hooks
初始發布。
修復循環錯誤。
不將拋出異常視為違反規則。
英文原文:
https://reactjs.org/blog/2019/02/06/react-v16.8.0.html?from=timeline&isappinstalled=0
推薦閱讀:React將引入Hooks,你怎麼看?
移動端上軟硬體在不斷升級,移動端上越來越廣泛的使用算法,比如人臉識別、背景分割等。面對這類新趨勢,ArchSummit 全球架構師峰會將邀請正在實施的團隊技術人來介紹最新成果。