fbpx
维基百科

React

React(也稱為React.js或ReactJS)是一個自由及开放源代码的前端JavaScript工具庫,[2] 用於基於UI組件構建用戶界面

React
原作者Jordan Walke
開發者Meta开源及其社区
首次发布2013年3月,​9年前​(2013-03
目前版本
  • 18.2.0 (2022年6月14日)[1]
源代码库github.com/facebook/react
编程语言JavaScript
系統平台跨平台
文件大小145 KiB生产版
726 KiB开发版
类型JavaScript函式庫
许可协议MIT許可證
网站reactjs.org/ 

它由Meta(前身為Facebook)和由個人開發者和公司組成的社群維護。[3][4][5] React可用作開發具有Next.js等框架的單頁、手機或伺服器渲染應用程式的基礎。然而,React只專注狀態管理和將狀態渲染到DOM,因此創建React應用程式通常需要使用額外的工具庫來進行路由實作,以及某些客戶端功能。[6][7]

基本使用方法

以下是使用JSX和JavaScript在HTML中使用React的基本範例。

import React from "react";  const Greeting = () => {  return (  <div className="hello_world">  <h1> Hello, world! </h1>  </div>  ); };  export default Greeting; 

Greeting函數是一個React組件,渲染的结果为“Hello, world”。

在網A瀏覽器中顯示時,結果將是以下內容的渲染:

<div class="hello_world"> <h1>Hello, world!</h1> </div> 

功能

宣告式語法

React採取宣告式程式撰寫範式。開發人員為應用程式的每個狀態設計視圖,React會在資料更改時更新和呈現組件。這與命令式程式撰寫不同。[8]

組件

React程式碼由稱為組件的實體組成。這些組件是可重複利用的,並且必須遵循大寫駝峰命名法(Pascal Case)作為其命名規則,也就是大寫版本的駝峰式命名法(camelCase)在src資料夾中形成。可以使用React DOM工具庫將組件渲染到DOM中的特定元素。渲染組件時,可以通過“props”在組件之間傳遞參數值:[9]

import React from "react"; import Tool from "./Tool"; const Example = () => { return ( <> <div className="app"> <Tool name="Gulshan" /> </div> </> ); }; export default Example; 

在上面的範例中,值為“Gulshan”的name屬性已從Example組件傳遞到Tool組件。

此外,return部分被包裝在一個名為return的標籤中,因為只能返回一個值。所以所有JSX元素和組件都綁定到一個標籤中。

在React中宣告組件的兩種主要方式是通過函數組件和基於類別組件。

函數組件

函數組件用一個函數聲明,然後返回一些JSX。

const Greeter = () => <div>Hello World</div>; 

類別組件

基於類的組件是使用ES6類別宣告。

class ParentComponent extends React.Component { state = { color: 'green' }; render() { return ( <ChildComponent color={this.state.color} /> ); } } 

類別組件都是關於類別的使用和生命週期方法的,而功能組件有Hooks來處理在React中編寫代碼時出現的狀態管理和其他問題。

虛擬DOM

另一個值得注意的特性是使用虛擬文件物件模型或虛擬DOM。React建立一個記憶體資料結構暫存,計算結果差異,然後有效地更新瀏覽器顯示的DOM。[10]這個過程稱為reconciliation。這允許程式工程師撰寫程式碼,就好像每次更改都會渲染整個頁面,而React只渲染實際更改的子組件。這種選擇性渲染提供了主要的性能提升。[11]節省了重新計算CSS樣式、頁面排版和渲染整個頁面的工作量。[11]

生命週期方法

基於類別組件的生命週期方法使用一種掛鉤(Hooking)形式,允許在組件生命週期內的設定點執行程式碼。

  • shouldComponentUpdate 允許開發人員通過在不需要渲染時返回false來防止不必要的組件重新渲染。
  • componentDidMount只要一旦組件「掛載(mounted)」(組件已在用戶界面中建立,通常通過將其與DOM節點關聯),就會被呼叫。這通常用於通過API觸發從遠端資料來源載入資料。
  • componentWillUnmount 在組件被移除或「卸載」(unmounted)之前立即被呼叫。通常用於清除對組件的資源需求相依性模組,這相依性模組不會隨著組件的卸載而容易地被刪除(例如,刪除與組件相關的任何 setInterval() ,或設定於“文件”的「事件監聽」(eventListener),因為組件的存在)。
  • render是最重要的生命週期方法,也是任何組件中唯一需要的方法。它通常在每次組件狀態更新時呼叫,也就是應該反映在用戶界面中。

JSX

JSX或JavaScript語法擴充,是JavaScript語言語法的擴充。[12] 在外表上與HTML類似,JSX提供了一種使用許多開發人員熟悉的語法來構建組件渲染的方法。React組件通常使用JSX撰寫,儘管它們並非必須如此(組件也可以使用純JavaScript撰寫)。JSX類似於Facebook為PHP創建的另一個擴展語法,稱為XHP。

JSX代碼範例:

class App extends React.Component { render() { return ( <div> <p>Header</p> <p>Content</p> <p>Footer</p> </div> ); } } 

HTML之外的架構

React的基本架構不僅適用於在瀏覽器中呈現HTML。例如,Facebook有渲染到<canvas>標籤,[13] NetflixPayPal 使用通用載入於伺服器和客戶端上渲染相同的HTML。[14][15]

React hooks

Hooks是讓開發人員從函數組件中“鉤入(Hook into)”React狀態和生命週期特性的函數。Hooks在類別組件無法作用——它們讓你在沒用類別組件情況下使用React。[16]

React提供了一些內建的Hook,例如 useState,[17]useContextuseReduceruseMemouseEffect[18] 其他的記錄在Hooks API參考中。[19] useStateuseEffect,也就是最常用,分別用於狀態和副作用的控制。

Hooks的使用規範

下面有一些Hooks規範[20] 描述了鉤子(Hooks)的特徵程式碼模式,也是現今使用React處理狀態方式。

  1. Hooks只能在頂層呼叫(而不是在迴圈或if語句內)。
  2. Hooks只能從React函數組件和自定義Hooks呼叫,而不是普通函數或類別組件。

雖然這些規範不能在執行時強制執行,但可以設定程式碼分析工具(例如linter)來偵測開發過程中的許多錯誤。

這些規範適用於Hooks的使用和自定義Hooks的實作,[21] 也就是可能會調用其他Hooks。

常用慣用語

React並不刻意提供一個完整的「應用程式工具庫」。專為構建用戶界面而設計,因此不包含一些開發人員可能認為構建應用程序所必需的許多工具。這也允許選擇開發人員喜歡的任何工具庫來完成諸如執行網路訪問或本地資料存儲等任務。隨著工具庫的成熟,常見的使用模式已經出現。

單向資料流

為了支援React的單向資料流概念(可能與 AngularJS 的雙向資料流形成對比),Flux架構被開發為流行的模型-視圖-控制器架構的替代方案。Flux具有通過中央調度程式發送到儲存區(Store)的操作,並且對儲存區的更改被傳遞回視圖。[22] 當與React一起使用時,這種傳遞是通過組件屬性完成的。從Flux的概念開始,Flux就被Redux和MobX等工具庫所取代。[23]

Flux可以被認為是觀察者模式的一種變體。[24]

Flux架構下的React組件不應直接修改傳遞給它的任何props,而應傳遞回調函數,這些回呼函數創建由調度程式發送的用於修改存儲的操作。動作(Action)是一個對象,負責描述發生的事情:例如,描述一個用戶“關注”另一個用戶的動作可能包含用戶ID、目標用戶ID和類型USER_FOLLOWED_ANOTHER_USER。可以將存儲視為模型,可以根據從調度程式接收到的操作來改變自己。

這種模式有時表示為“屬性(Properties)向下流動,動作(Actions)向上流動”。Flux的許多實作從一開始就被建立了,也許最著名的是Redux,它具有單一存儲,通常稱為單一資料源。[25]

未來發展

可以通過核心團隊討論論壇追踪專案狀態。[26] 但是,對React的重大更改會通過React存儲庫的未來問題和拉取請求(Pull request)。[27][28] 這使React社群能夠就新的潛在功能、實驗性API和JavaScript語法改進提供回饋。

歷史

React由Facebook的軟體工程師Jordan Walke建立,他發布了一個名為“FaxJS”的React早期原型。[29][30] 他受到XHP(一個PHP的HTML組件庫)的影響。於2011年首次部署在Facebook的News Feed上,隨後於2012年部署在Instagram上。[31] 於2013年5月在JSConf US宣布開放原始碼。[30]

React Native 於2015年2月在Facebook的React Conf上宣布,並於2015年3月開放原始碼,支持使用React進行原生Android、iOS和UWP開發。

2017年4月18日,Facebook發布了React Fiber,這是一套新的內部渲染演算法,與React的舊渲染演算法Stack不同。React Fiber將成為React工具庫未來任何改進和功能開發的基礎。[32][已过时] 使用React程式撰寫的實際語法不會改變;只有語法的執行方式發生了變化。[33] React的舊渲染系統Stack是在不了解系統對動態變化的關注點的時候開發的。Stack繪製複雜動畫的速度很緩慢,例如,試圖在一個塊中完成所有動畫。Fiber將動畫分解為可以分佈在多個影格上的片段。同樣,一個頁面的結構可以分解為可以單獨維護和更新的片段。JavaScript函數和虛擬DOM對像被稱為“纖程”,每個都可以單獨操作和更新,從而實現更流暢的屏幕渲染。[34]

2017年9月26日,React 16.0向公眾發布。[35]

2019年2月16日,React 16.8向公眾發布。[36] 該版本導入了React Hooks。[37]

2020年8月10日,React團隊宣布了React v17.0的第一個候選版本,值得關注的是第一個主要版本沒有對面向React開發人員的API進行重大更改。[38]

授權

2013年5月React的首次公開發布使用了Apache License 2.0。2014年10月,React 0.12.0將其更換為3言條款BSD授權條欺,並新增了一個單獨的專利授權文件,允許使用與該軟體相關的任何Facebook專利:

The license granted hereunder will terminate, automatically and without notice, for anyone that makes any claim (including by filing any lawsuit, assertion or other action) alleging (a) direct, indirect, or contributory infringement or inducement to infringe any patent: (i) by Facebook or any of its subsidiaries or affiliates, whether or not such claim is related to the Software, (ii) by any party if such claim arises in whole or in part from any software, product or service of Facebook or any of its subsidiaries or affiliates, whether or not such claim is related to the Software, or (iii) by any party relating to the Software; or (b) that any right in any patent claim of Facebook is invalid or unenforceable.

這個不同以往的條款在React用戶社群引起了一些爭議和爭論,因為可以被解釋為授權Facebook在許多情況下撤銷授權,例如,如果Facebook起訴被授權人通過發布動作來提示他們採取“其他行動”在部落格或其他地方。許多人擔心Facebook可能會不公平地利用終止條款,或者將React集成到產品中可能會使新創公司未來的收購變得複雜。[39]

根據社群回饋,Facebook於2015年4月更新了專利授權,以減少歧義並更加寬容:[40]

The license granted hereunder will terminate, automatically and without notice, if you (or any of your subsidiaries, corporate affiliates or agents) initiate directly or indirectly, or take a direct financial interest in, any Patent Assertion: (i) against Facebook or any of its subsidiaries or corporate affiliates, (ii) against any party if such Patent Assertion arises in whole or in part from any software, technology, product or service of Facebook or any of its subsidiaries or corporate affiliates, or (iii) against any party relating to the Software. [...] A "Patent Assertion" is any lawsuit or other action alleging direct, indirect, or contributory infringement or inducement to infringe any patent, including a cross-claim or counterclaim.[41]

Apache軟件基金會認為這種授權條款安排與其授權政策不相容,因為它“將風險轉嫁給我們軟體的下游消費者,而不是有利於權利擁有者,而不是權利被被授權者,從而違反了我們作為通用捐助者的Apache法律政策”和“不是 [Apache授權2.0] 中的子集,它們不能作為 [Apache授權2.0] 再授權”。[42] 2017年8月,Facebook駁回了Apache基金會對下游的擔憂,並拒絕重新考慮他們的授權條款。[43][44] 接下來的一個月,WordPress 決定將其Gutenberg和Calypso專案從React中移除。[45]

2017年9月23日,Facebook宣布下週將根據標準MIT授權條款重新授權Flow、Jest、React和Immutable.js;該公司表示,React是“網絡開放原始碼軟體廣泛生態系統的基礎”,他們不想“因為非技術原因阻礙進展”。[46]

2017年9月26日,React 16.0.0以MIT授權條款發布。[47] MIT授權條款更改也已通過React 15.6.2向後移植到15.x版本線。[48]

参见

参考资料

  1. ^ 1.0 1.1 18.2.0 (June 14, 2022). [2022年6月23日]. 
  2. ^ React - A JavaScript library for building user interfaces.. React. [7 April 2018]. (原始内容于2022-04-11). 
  3. ^ Krill, Paul. React: Making faster, smoother UIs for data-driven Web apps. InfoWorld. May 15, 2014 [2022-05-19]. (原始内容于2018-06-12). 
  4. ^ Hemel, Zef. Facebook's React JavaScript User Interfaces Library Receives Mixed Reviews. InfoQ. June 3, 2013 [2022-05-19]. (原始内容于2018-06-12). 
  5. ^ Dawson, Chris. JavaScript's History and How it Led To ReactJS. The New Stack. July 25, 2014 [2022-05-19]. (原始内容于2018-06-12). 
  6. ^ Dere, Mohan. How to integrate create-react-app with all the libraries you need to make a great app. freeCodeCamp. 2018-02-19 [2018-06-14]. 
  7. ^ Angular vs React Detailed Comparison. Groovy Web. 2018-02-19 [2022-04-25]. (原始内容于2022-05-31). 
  8. ^ Schwarzmüller, Max. React - The Complete Guide. O'Reilly. Packt Publishing. [19 February 2022]. (原始内容于2022-05-31). 
  9. ^ Components and Props. React. Facebook. [7 April 2018]. (原始内容于2022-08-07). 
  10. ^ Refs and the DOM. React Blog. [2022-05-19]. (原始内容于2022-08-07). 
  11. ^ 11.0 11.1 React: The Virtual DOM. Codecademy. [2021-10-14]. (原始内容于2021-10-28) (英语). 
  12. ^ Draft: JSX Specification. JSX. Facebook. [7 April 2018]. (原始内容于2022-04-02). 
  13. ^ Why did we build React? – React Blog. [2022-05-19]. (原始内容于2015-04-06). 
  14. ^ PayPal Isomorphic React. (原始内容于2019-02-08). 
  15. ^ Netflix Isomorphic React. [2022-05-19]. (原始内容于2016-12-17). 
  16. ^ What the Heck is React Hooks?. Soshace. 2020-01-16 [2020-01-24]. (原始内容于2022-05-31) (英语). 
  17. ^ Using the State Hook – React. reactjs.org. [2020-01-24]. (原始内容于2022-07-30) (英语). 
  18. ^ Using the Effect Hook – React. reactjs.org. [2020-01-24]. (原始内容于2022-08-01) (英语). 
  19. ^ Hooks API Reference – React. reactjs.org. [2020-01-24]. (原始内容于2022-08-05) (英语). 
  20. ^ Rules of Hooks – React. reactjs.org. [2020-01-24]. (原始内容于2021-06-06) (英语). 
  21. ^ Building Your Own Hooks – React. reactjs.org. [2020-01-24]. (原始内容于2022-07-17) (英语). 
  22. ^ In Depth OverView. Flux. Facebook. [7 April 2018]. (原始内容于2022-08-07). 
  23. ^ Flux Release 4.0. Github. [26 February 2021]. (原始内容于2022-05-31). 
  24. ^ Johnson, Nicholas. Introduction to Flux - React Exercise. Nicholas Johnson. [7 April 2018]. (原始内容于2022-05-31). 
  25. ^ State Management Tools - Results. The State of JavaScript. [29 October 2021]. (原始内容于2022-05-31). 
  26. ^ Meeting Notes. React Discuss. [2015-12-13]. (原始内容于2015-12-22). 
  27. ^ reactjs/react-future - The Future of React. GitHub. [2015-12-13]. (原始内容于2022-07-13). 
  28. ^ facebook/react - Feature request issues. GitHub. [2015-12-13]. (原始内容于2022-07-09). 
  29. ^ Walke, Jordan. FaxJS. [11 July 2019]. (原始内容于2021-12-16). 
  30. ^ 30.0 30.1 Papp, Andrea. The History of React.js on a Timeline. RisingStack. 4 April 2018 [11 July 2019]. (原始内容于2022-05-31). 
  31. ^ Pete Hunt at TXJS. [2022-05-19]. (原始内容于2017-07-31). 
  32. ^ React Fiber Architecture. Github. [19 April 2017]. (原始内容于2018-05-10). 
  33. ^ Facebook announces React Fiber, a rewrite of its React framework. TechCrunch. [2018-10-19]. (原始内容于2018-06-14). 
  34. ^ GitHub - acdlite/react-fiber-architecture: A description of React's new core algorithm, React Fiber. github.com. [2018-10-19]. (原始内容于2018-05-10). 
  35. ^ React v16.0. react.js. 2017-09-26 [2019-05-20]. (原始内容于2022-07-14). 
  36. ^ React v16.8. react.js. 2019-02-16 [2019-05-20]. (原始内容于2022-07-14). 
  37. ^ Introducing Hooks. react.js. [2019-05-20]. (原始内容于2022-08-03). 
  38. ^ url=https://reactjs.org/blog/2020/08/10/react-v17-rc.html (页面存档备份,存于互联网档案馆
  39. ^ Liu, Austin. A compelling reason not to use ReactJS. Medium. [2022-05-19]. (原始内容于2022-05-31). 
  40. ^ Updating Our Open Source Patent Grant. [2022-05-19]. (原始内容于2020-11-08). 
  41. ^ Additional Grant of Patent Rights Version 2. GitHub. [2022-05-19]. (原始内容于2022-05-31). 
  42. ^ ASF Legal Previously Asked Questions. Apache Software Foundation. [2017-07-16]. (原始内容于2018-02-06) (英语). 
  43. ^ Explaining React's License. Facebook. [2017-08-18]. (原始内容于2021-05-06) (英语). 
  44. ^ Consider re-licensing to AL v2.0, as RocksDB has just done. Github. [2017-08-18]. (原始内容于2022-07-27) (英语). 
  45. ^ WordPress to ditch React library over Facebook patent clause risk. TechCrunch. [2017-09-16]. (原始内容于2022-05-31) (英语). 
  46. ^ Relicensing React, Jest, Flow, and Immutable.js. Facebook Code. 2017-09-23 [2022-05-19]. (原始内容于2020-12-06) (英语). 
  47. ^ Clark, Andrew. React v16.0§MIT licensed. React Blog. September 26, 2017 [2022-05-19]. (原始内容于2022-07-14). 
  48. ^ Hunzaker, Nathan. React v15.6.2. React Blog. September 25, 2017 [2022-05-19]. (原始内容于2022-05-31). 

外部链接

react, 此條目可参照外語維基百科相應條目来扩充, 若您熟悉来源语言和主题, 请协助参考外语维基百科扩充条目, 请勿直接提交机械翻译, 也不要翻译不可靠, 低品质内容, 依版权协议, 译文需在编辑摘要注明来源, 或于讨论页顶部标记, href, template, translated, page, html, title, template, translated, page, translated, page, 标签, 也稱為, js或js, 是一個自由及开放源代码的前端javascript工具庫, 用於基. 此條目可参照外語維基百科相應條目来扩充 若您熟悉来源语言和主题 请协助参考外语维基百科扩充条目 请勿直接提交机械翻译 也不要翻译不可靠 低品质内容 依版权协议 译文需在编辑摘要注明来源 或于讨论页顶部标记 a href Template Translated page html title Template Translated page Translated page a 标签 React 也稱為React js或ReactJS 是一個自由及开放源代码的前端JavaScript工具庫 2 用於基於UI組件構建用戶界面 React原作者Jordan Walke開發者Meta开源及其社区首次发布2013年3月 9年前 2013 03 目前版本18 2 0 2022年6月14日 1 源代码库github wbr com wbr facebook wbr react编程语言JavaScript系統平台跨平台文件大小145 KiB生产版726 KiB开发版类型JavaScript函式庫许可协议MIT許可證网站reactjs wbr org 它由Meta 前身為Facebook 和由個人開發者和公司組成的社群維護 3 4 5 React可用作開發具有Next js等框架的單頁 手機或伺服器渲染應用程式的基礎 然而 React只專注狀態管理和將狀態渲染到DOM 因此創建React應用程式通常需要使用額外的工具庫來進行路由實作 以及某些客戶端功能 6 7 目录 1 基本使用方法 2 功能 2 1 宣告式語法 2 2 組件 2 3 函數組件 2 4 類別組件 2 5 虛擬DOM 2 6 生命週期方法 2 7 JSX 2 8 HTML之外的架構 2 9 React hooks 2 9 1 Hooks的使用規範 3 常用慣用語 3 1 單向資料流 4 未來發展 5 歷史 6 授權 7 参见 8 参考资料 9 外部链接基本使用方法 编辑以下是使用JSX和JavaScript在HTML中使用React的基本範例 import React from react const Greeting gt return lt div className hello world gt lt h1 gt Hello world lt h1 gt lt div gt export default Greeting Greeting函數是一個React組件 渲染的结果为 Hello world 在網A瀏覽器中顯示時 結果將是以下內容的渲染 lt div class hello world gt lt h1 gt Hello world lt h1 gt lt div gt 功能 编辑宣告式語法 编辑 React採取宣告式程式撰寫範式 開發人員為應用程式的每個狀態設計視圖 React會在資料更改時更新和呈現組件 這與命令式程式撰寫不同 8 組件 编辑 React程式碼由稱為組件的實體組成 這些組件是可重複利用的 並且必須遵循大寫駝峰命名法 Pascal Case 作為其命名規則 也就是大寫版本的駝峰式命名法 camelCase 在src資料夾中形成 可以使用React DOM工具庫將組件渲染到DOM中的特定元素 渲染組件時 可以通過 props 在組件之間傳遞參數值 9 import React from react import Tool from Tool const Example gt return lt gt lt div className app gt lt Tool name Gulshan gt lt div gt lt gt export default Example 在上面的範例中 值為 Gulshan 的name屬性已從Example組件傳遞到Tool組件 此外 return部分被包裝在一個名為return的標籤中 因為只能返回一個值 所以所有JSX元素和組件都綁定到一個標籤中 在React中宣告組件的兩種主要方式是通過函數組件和基於類別組件 函數組件 编辑 函數組件用一個函數聲明 然後返回一些JSX const Greeter gt lt div gt Hello World lt div gt 類別組件 编辑 基於類的組件是使用ES6類別宣告 class ParentComponent extends React Component state color green render return lt ChildComponent color this state color gt 類別組件都是關於類別的使用和生命週期方法的 而功能組件有Hooks來處理在React中編寫代碼時出現的狀態管理和其他問題 虛擬DOM 编辑 另一個值得注意的特性是使用虛擬文件物件模型或虛擬DOM React建立一個記憶體資料結構暫存 計算結果差異 然後有效地更新瀏覽器顯示的DOM 10 這個過程稱為reconciliation 這允許程式工程師撰寫程式碼 就好像每次更改都會渲染整個頁面 而React只渲染實際更改的子組件 這種選擇性渲染提供了主要的性能提升 11 節省了重新計算CSS樣式 頁面排版和渲染整個頁面的工作量 11 生命週期方法 编辑 基於類別組件的生命週期方法使用一種掛鉤 Hooking 形式 允許在組件生命週期內的設定點執行程式碼 shouldComponentUpdate 允許開發人員通過在不需要渲染時返回false來防止不必要的組件重新渲染 componentDidMount只要一旦組件 掛載 mounted 組件已在用戶界面中建立 通常通過將其與DOM節點關聯 就會被呼叫 這通常用於通過API觸發從遠端資料來源載入資料 componentWillUnmount 在組件被移除或 卸載 unmounted 之前立即被呼叫 通常用於清除對組件的資源需求相依性模組 這相依性模組不會隨著組件的卸載而容易地被刪除 例如 刪除與組件相關的任何 setInterval 或設定於 文件 的 事件監聽 eventListener 因為組件的存在 render是最重要的生命週期方法 也是任何組件中唯一需要的方法 它通常在每次組件狀態更新時呼叫 也就是應該反映在用戶界面中 JSX 编辑 JSX或JavaScript語法擴充 是JavaScript語言語法的擴充 12 在外表上與HTML類似 JSX提供了一種使用許多開發人員熟悉的語法來構建組件渲染的方法 React組件通常使用JSX撰寫 儘管它們並非必須如此 組件也可以使用純JavaScript撰寫 JSX類似於Facebook為PHP創建的另一個擴展語法 稱為XHP JSX代碼範例 class App extends React Component render return lt div gt lt p gt Header lt p gt lt p gt Content lt p gt lt p gt Footer lt p gt lt div gt HTML之外的架構 编辑 React的基本架構不僅適用於在瀏覽器中呈現HTML 例如 Facebook有渲染到 lt canvas gt 標籤 13 Netflix 和 PayPal 使用通用載入於伺服器和客戶端上渲染相同的HTML 14 15 React hooks 编辑 Hooks是讓開發人員從函數組件中 鉤入 Hook into React狀態和生命週期特性的函數 Hooks在類別組件無法作用 它們讓你在沒用類別組件情況下使用React 16 React提供了一些內建的Hook 例如 useState 17 useContext useReducer useMemo 與useEffect 18 其他的記錄在Hooks API參考中 19 useState 與useEffect 也就是最常用 分別用於狀態和副作用的控制 Hooks的使用規範 编辑 下面有一些Hooks規範 20 描述了鉤子 Hooks 的特徵程式碼模式 也是現今使用React處理狀態方式 Hooks只能在頂層呼叫 而不是在迴圈或if語句內 Hooks只能從React函數組件和自定義Hooks呼叫 而不是普通函數或類別組件 雖然這些規範不能在執行時強制執行 但可以設定程式碼分析工具 例如linter 來偵測開發過程中的許多錯誤 這些規範適用於Hooks的使用和自定義Hooks的實作 21 也就是可能會調用其他Hooks 常用慣用語 编辑React並不刻意提供一個完整的 應用程式工具庫 專為構建用戶界面而設計 因此不包含一些開發人員可能認為構建應用程序所必需的許多工具 這也允許選擇開發人員喜歡的任何工具庫來完成諸如執行網路訪問或本地資料存儲等任務 隨著工具庫的成熟 常見的使用模式已經出現 單向資料流 编辑 為了支援React的單向資料流概念 可能與 AngularJS 的雙向資料流形成對比 Flux架構被開發為流行的模型 視圖 控制器架構的替代方案 Flux具有通過中央調度程式發送到儲存區 Store 的操作 並且對儲存區的更改被傳遞回視圖 22 當與React一起使用時 這種傳遞是通過組件屬性完成的 從Flux的概念開始 Flux就被Redux和MobX等工具庫所取代 23 Flux可以被認為是觀察者模式的一種變體 24 Flux架構下的React組件不應直接修改傳遞給它的任何props 而應傳遞回調函數 這些回呼函數創建由調度程式發送的用於修改存儲的操作 動作 Action 是一個對象 負責描述發生的事情 例如 描述一個用戶 關注 另一個用戶的動作可能包含用戶ID 目標用戶ID和類型USER FOLLOWED ANOTHER USER 可以將存儲視為模型 可以根據從調度程式接收到的操作來改變自己 這種模式有時表示為 屬性 Properties 向下流動 動作 Actions 向上流動 Flux的許多實作從一開始就被建立了 也許最著名的是Redux 它具有單一存儲 通常稱為單一資料源 25 未來發展 编辑可以通過核心團隊討論論壇追踪專案狀態 26 但是 對React的重大更改會通過React存儲庫的未來問題和拉取請求 Pull request 27 28 這使React社群能夠就新的潛在功能 實驗性API和JavaScript語法改進提供回饋 歷史 编辑React由Facebook的軟體工程師Jordan Walke建立 他發布了一個名為 FaxJS 的React早期原型 29 30 他受到XHP 一個PHP的HTML組件庫 的影響 於2011年首次部署在Facebook的News Feed上 隨後於2012年部署在Instagram上 31 於2013年5月在JSConf US宣布開放原始碼 30 React Native 於2015年2月在Facebook的React Conf上宣布 並於2015年3月開放原始碼 支持使用React進行原生Android iOS和UWP開發 2017年4月18日 Facebook發布了React Fiber 這是一套新的內部渲染演算法 與React的舊渲染演算法Stack不同 React Fiber將成為React工具庫未來任何改進和功能開發的基礎 32 已过时 使用React程式撰寫的實際語法不會改變 只有語法的執行方式發生了變化 33 React的舊渲染系統Stack是在不了解系統對動態變化的關注點的時候開發的 Stack繪製複雜動畫的速度很緩慢 例如 試圖在一個塊中完成所有動畫 Fiber將動畫分解為可以分佈在多個影格上的片段 同樣 一個頁面的結構可以分解為可以單獨維護和更新的片段 JavaScript函數和虛擬DOM對像被稱為 纖程 每個都可以單獨操作和更新 從而實現更流暢的屏幕渲染 34 2017年9月26日 React 16 0向公眾發布 35 2019年2月16日 React 16 8向公眾發布 36 該版本導入了React Hooks 37 2020年8月10日 React團隊宣布了React v17 0的第一個候選版本 值得關注的是第一個主要版本沒有對面向React開發人員的API進行重大更改 38 授權 编辑2013年5月React的首次公開發布使用了Apache License 2 0 2014年10月 React 0 12 0將其更換為3言條款BSD授權條欺 並新增了一個單獨的專利授權文件 允許使用與該軟體相關的任何Facebook專利 The license granted hereunder will terminate automatically and without notice for anyone that makes any claim including by filing any lawsuit assertion or other action alleging a direct indirect or contributory infringement or inducement to infringe any patent i by Facebook or any of its subsidiaries or affiliates whether or not such claim is related to the Software ii by any party if such claim arises in whole or in part from any software product or service of Facebook or any of its subsidiaries or affiliates whether or not such claim is related to the Software or iii by any party relating to the Software or b that any right in any patent claim of Facebook is invalid or unenforceable 這個不同以往的條款在React用戶社群引起了一些爭議和爭論 因為可以被解釋為授權Facebook在許多情況下撤銷授權 例如 如果Facebook起訴被授權人通過發布動作來提示他們採取 其他行動 在部落格或其他地方 許多人擔心Facebook可能會不公平地利用終止條款 或者將React集成到產品中可能會使新創公司未來的收購變得複雜 39 根據社群回饋 Facebook於2015年4月更新了專利授權 以減少歧義並更加寬容 40 The license granted hereunder will terminate automatically and without notice if you or any of your subsidiaries corporate affiliates or agents initiate directly or indirectly or take a direct financial interest in any Patent Assertion i against Facebook or any of its subsidiaries or corporate affiliates ii against any party if such Patent Assertion arises in whole or in part from any software technology product or service of Facebook or any of its subsidiaries or corporate affiliates or iii against any party relating to the Software A Patent Assertion is any lawsuit or other action alleging direct indirect or contributory infringement or inducement to infringe any patent including a cross claim or counterclaim 41 Apache軟件基金會認為這種授權條款安排與其授權政策不相容 因為它 將風險轉嫁給我們軟體的下游消費者 而不是有利於權利擁有者 而不是權利被被授權者 從而違反了我們作為通用捐助者的Apache法律政策 和 不是 Apache授權2 0 中的子集 它們不能作為 Apache授權2 0 再授權 42 2017年8月 Facebook駁回了Apache基金會對下游的擔憂 並拒絕重新考慮他們的授權條款 43 44 接下來的一個月 WordPress 決定將其Gutenberg和Calypso專案從React中移除 45 2017年9月23日 Facebook宣布下週將根據標準MIT授權條款重新授權Flow Jest React和Immutable js 該公司表示 React是 網絡開放原始碼軟體廣泛生態系統的基礎 他們不想 因為非技術原因阻礙進展 46 2017年9月26日 React 16 0 0以MIT授權條款發布 47 MIT授權條款更改也已通過React 15 6 2向後移植到15 x版本線 48 参见 编辑Vue js AngularJS参考资料 编辑 1 0 1 1 18 2 0 June 14 2022 2022年6月23日 React A JavaScript library for building user interfaces React 7 April 2018 原始内容存档于2022 04 11 Krill Paul React Making faster smoother UIs for data driven Web apps InfoWorld May 15 2014 2022 05 19 原始内容存档于2018 06 12 Hemel Zef Facebook s React JavaScript User Interfaces Library Receives Mixed Reviews InfoQ June 3 2013 2022 05 19 原始内容存档于2018 06 12 Dawson Chris JavaScript s History and How it Led To ReactJS The New Stack July 25 2014 2022 05 19 原始内容存档于2018 06 12 Dere Mohan How to integrate create react app with all the libraries you need to make a great app freeCodeCamp 2018 02 19 2018 06 14 Angular vs React Detailed Comparison Groovy Web 2018 02 19 2022 04 25 原始内容存档于2022 05 31 Schwarzmuller Max React The Complete Guide O Reilly Packt Publishing 19 February 2022 原始内容存档于2022 05 31 Components and Props React Facebook 7 April 2018 原始内容存档于2022 08 07 Refs and the DOM React Blog 2022 05 19 原始内容存档于2022 08 07 11 0 11 1 React The Virtual DOM Codecademy 2021 10 14 原始内容存档于2021 10 28 英语 Draft JSX Specification JSX Facebook 7 April 2018 原始内容存档于2022 04 02 Why did we build React React Blog 2022 05 19 原始内容存档于2015 04 06 PayPal Isomorphic React 原始内容存档于2019 02 08 Netflix Isomorphic React 2022 05 19 原始内容存档于2016 12 17 What the Heck is React Hooks Soshace 2020 01 16 2020 01 24 原始内容存档于2022 05 31 英语 Using the State Hook React reactjs org 2020 01 24 原始内容存档于2022 07 30 英语 Using the Effect Hook React reactjs org 2020 01 24 原始内容存档于2022 08 01 英语 Hooks API Reference React reactjs org 2020 01 24 原始内容存档于2022 08 05 英语 Rules of Hooks React reactjs org 2020 01 24 原始内容存档于2021 06 06 英语 Building Your Own Hooks React reactjs org 2020 01 24 原始内容存档于2022 07 17 英语 In Depth OverView Flux Facebook 7 April 2018 原始内容存档于2022 08 07 Flux Release 4 0 Github 26 February 2021 原始内容存档于2022 05 31 Johnson Nicholas Introduction to Flux React Exercise Nicholas Johnson 7 April 2018 原始内容存档于2022 05 31 State Management Tools Results The State of JavaScript 29 October 2021 原始内容存档于2022 05 31 Meeting Notes React Discuss 2015 12 13 原始内容存档于2015 12 22 reactjs react future The Future of React GitHub 2015 12 13 原始内容存档于2022 07 13 facebook react Feature request issues GitHub 2015 12 13 原始内容存档于2022 07 09 Walke Jordan FaxJS 11 July 2019 原始内容存档于2021 12 16 30 0 30 1 Papp Andrea The History of React js on a Timeline RisingStack 4 April 2018 11 July 2019 原始内容存档于2022 05 31 Pete Hunt at TXJS 2022 05 19 原始内容存档于2017 07 31 React Fiber Architecture Github 19 April 2017 原始内容存档于2018 05 10 Facebook announces React Fiber a rewrite of its React framework TechCrunch 2018 10 19 原始内容存档于2018 06 14 GitHub acdlite react fiber architecture A description of React s new core algorithm React Fiber github com 2018 10 19 原始内容存档于2018 05 10 React v16 0 react js 2017 09 26 2019 05 20 原始内容存档于2022 07 14 React v16 8 react js 2019 02 16 2019 05 20 原始内容存档于2022 07 14 Introducing Hooks react js 2019 05 20 原始内容存档于2022 08 03 url https reactjs org blog 2020 08 10 react v17 rc html 页面存档备份 存于互联网档案馆 Liu Austin A compelling reason not to use ReactJS Medium 2022 05 19 原始内容存档于2022 05 31 Updating Our Open Source Patent Grant 2022 05 19 原始内容存档于2020 11 08 Additional Grant of Patent Rights Version 2 GitHub 2022 05 19 原始内容存档于2022 05 31 ASF Legal Previously Asked Questions Apache Software Foundation 2017 07 16 原始内容存档于2018 02 06 英语 Explaining React s License Facebook 2017 08 18 原始内容存档于2021 05 06 英语 Consider re licensing to AL v2 0 as RocksDB has just done Github 2017 08 18 原始内容存档于2022 07 27 英语 WordPress to ditch React library over Facebook patent clause risk TechCrunch 2017 09 16 原始内容存档于2022 05 31 英语 Relicensing React Jest Flow and Immutable js Facebook Code 2017 09 23 2022 05 19 原始内容存档于2020 12 06 英语 Clark Andrew React v16 0 MIT licensed React Blog September 26 2017 2022 05 19 原始内容存档于2022 07 14 Hunzaker Nathan React v15 6 2 React Blog September 25 2017 2022 05 19 原始内容存档于2022 05 31 外部链接 编辑 自由软件主题 官方网站 Reactjs Example 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title React amp oldid 73928203, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。