请输入您要查询的百科知识:

 

词条 React (JavaScript library)
释义

  1. History

  2. Basic usage

  3. Notable features

     One-way data binding with props   Stateful components   Virtual DOM   Lifecycle methods   JSX  Architecture beyond HTML 

  4. Common idioms

     Use of the Flux architecture 

  5. Future development

     Sub projects  Facebook Contributor License Agreement (CLA) 

  6. Criticism

  7. Licensing controversy

  8. See also

  9. References

  10. External links

{{Infobox software
| name = React
| logo = React-icon.svg
| author = Jordan Walke
| developer = Facebook and community
| released = {{Start date and age|2013|5|29}}[1]
| latest release version = 16.8.4
| latest release date = {{Start date and age|2019|03|05}}[2]
| latest preview version =
| latest preview date = | programming language = JavaScript
| platform = Web platform
| size = 109.7 KiB production
774.7 KiB development
| genre = JavaScript library
| license = MIT License
| website = {{URL|reactjs.org}}
}}React (also known as React.js or ReactJS) is a JavaScript library[3] for building user interfaces. It is maintained by Facebook and a community of individual developers and companies.[4][5][6]

React can be used as a base in the development of single-page or mobile applications. Complex React applications usually require the use of additional libraries for state management, routing, and interaction with an API.[7][8]

History

React was created by Jordan Walke, a software engineer at Facebook. He was influenced by XHP, an HTML component framework for PHP.[9] It was first deployed on Facebook's newsfeed in 2011 and later on Instagram.com in 2012.[10] It was open-sourced at JSConf US in May 2013.

React Native, which enables native Android, iOS, and UWP development with React, was announced at Facebook's React.js Conf in February 2015 and open-sourced in March 2015.

On April 18, 2017, Facebook announced React Fiber, a new core algorithm of React framework library for building user interfaces.[11] React Fiber was to become the foundation of any future improvements and feature development of the React framework.[12]{{Update inline|reason=Last commit was in 2016. Is this statement still true?|date=June 2018}}

On April 19, 2017, React 360 V1.0.0 was released to the public.[13] This allowed developers with experience using react to jump into VR development.

Basic usage

The following is a rudimentary example of React usage in HTML with JSX and JavaScript.

The Greeter class is a React component that accepts a property greeting. The ReactDOM.render method creates an instance of the Greeter component, sets the greeting property to 'Hello World' and inserts the rendered component as a child element to the DOM element with id myReactApp.

When displayed in a web browser the result will be

Hello World!

Notable features

One-way data binding with props

Properties (commonly, props) are passed to a component from the parent component. Components receive props as a single set of immutable values[14] (a JavaScript object).

Stateful components

States hold values throughout the component and can be passed to child components through props:

class ParentComponent extends React.Component {

  state = { color: 'green' };  render() {    return (
    );  }

}

Virtual DOM

Another notable feature is the use of a "virtual Document Object Model", or "virtual DOM". React creates an in-memory data-structure cache, computes the resulting differences, and then updates the browser's displayed DOM efficiently.[15] This allows the programmer to write code as if the entire page is rendered on each change, while the React libraries only render subcomponents that actually change.

Lifecycle methods

Lifecycle methods are hooks which allow execution of code at set points during the component's lifetime.

  • shouldComponentUpdate allows the developer to prevent unnecessary re-rendering of a component by returning false if a render is not required.
  • componentDidMount is called once the component has 'mounted' (the component has been created in the user interface, often by associating it with a DOM node). This is commonly used to trigger data loading from a remote source via an API.
  • componentWillUnmount is called immediately before the component is tore down or 'unmounted'. This is commonly used to clear resource demanding dependencies to the component that will not simply be removed with the unmounting of the component (e.g. removing any 'setInterval()' instances that are related to the component, or an 'eventListener' set on the 'document' because of the presence of the component)
  • render is the most important lifecycle method and the only required one in any component. It is usually called every time the component's state is updated, reflecting changes in the user interface.

JSX

JSX (JavaScript XML) is an extension to the JavaScript language syntax.[16] Similar in appearance to HTML, JSX provides a way to structure component rendering using syntax familiar to many developers. React components are typically written using JSX, although they do not have to be (components may also be written in pure JavaScript). JSX is similar to another extension syntax created by Facebook for PHP, XHP.

An example of JSX code:

class App extends React.Component {

  render() {    return (

Header

Content

Footer

    );  }

}

Nested elements

Multiple elements on the same level need to be wrapped in a single container element such as the <div> element shown above, or returned as an array[17].

Attributes

JSX provides a range of element attributes designed to mirror those provided by HTML. Custom attributes can also be passed to the component[18]. All attributes will be received by the component as props.

JavaScript expressions

JavaScript expressions (but not statements) can be used inside JSX with curly brackets {}:

{10+1}

The example above will render

11

Conditional statements

If–else statements cannot be used inside JSX but conditional expressions can be used instead.

The example below will render { i === 1 ? 'true' : 'false' } as the string 'true' because i is equal to 1.

class App extends React.Component {

  render() {    const i = 1;    return (

{ i === 1 ? 'true' : 'false' }

    );  }

}

The above will render:

true

Functions and JSX can be used in conditionals:

class App extends React.Component {

  render() {    const sections = [1, 2, 3];    return (
        {sections.length > 0 && sections.map(n => (            /* 'key' is used by react to keep track of list items and their changes */
Section {n}
    );  }

}

The above will render:

Section 1
Section 2
Section 3

Code written in JSX requires conversion with a tool such as Babel before it can be understood by web browsers.[19] This processing is generally performed during a software build process before the application is deployed.

Architecture beyond HTML

The basic architecture of React applies beyond rendering HTML in the browser. For example, Facebook has dynamic charts that render to <canvas> tags,[20] and Netflix and PayPal use universal loading to render identical HTML on both the server and client.[21][22]

Common idioms

React does not attempt to provide a complete 'application framework'. It is designed specifically for building user interfaces[3] and therefore does not include many of the tools some developers might consider necessary to build an application. This allows the choice of whichever libraries the developer prefers to accomplish tasks such as performing network access or local data storage. Common patterns of usage have emerged as the library matures.

Use of the Flux architecture

To support React's concept of unidirectional data flow (which might be contrasted with AngularJS's bidirectional flow), the Flux architecture represents an alternative to the popular model-view-controller architecture. Flux features actions which are sent through a central dispatcher to a store, and changes to the store are propagated back to the view[23]. When used with React, this propagation is accomplished through component properties.

Flux can be considered a variant of the observer pattern[24].

A React component under the Flux architecture should not directly modify any props passed to it, but should be passed callback functions that create actions which are sent by the dispatcher to modify the store. The action is an object whose responsibility is to describe what has taken place: for example, an action describing one user 'following' another might contain a user id, a target user id, and the type USER_FOLLOWED_ANOTHER_USER[25]. The stores (which can be thought of as models) can alter themselves in response to actions received from the dispatcher.

This pattern is sometimes expressed as "properties flow down, actions flow up". Many implementations of Flux have been created since its inception, perhaps the most well-known being Redux which features a single store, often called a single source of truth.[26]

Future development

Project status can be tracked via the core team discussion forum.[27] However, major changes to React go through the Future of React repository issues and pull requests.[28][29] This enables the React community to provide feedback on new potential features, experimental APIs and JavaScript syntax improvements.

Sub projects

The status of the React sub-projects used to be available in the project wiki.[30]

Facebook Contributor License Agreement (CLA)

Facebook requires contributors to React to sign the Facebook CLA.[31][32]

Criticism

A criticism of React is that it has high memory (RAM) requirements, since it uses the concept of a "Virtual DOM". This is where "a representation of a UI is kept in memory and synced with the 'real' DOM by a library such as ReactDOM".[33]

As well, due to its Virtual DOM abstraction, React versions including 16 work poorly with the browser's in-built component model[34], and thus with alternative libraries which rely on browser standards to implement their components.

Licensing controversy

The initial public release of React in May 2013 used a standard Apache License 2.0. In October 2014, React 0.12.0 replaced this with a 3-clause BSD license and added a separate PATENTS text file that permits usage of any Facebook patents related to the software:[35]

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.

This unconventional clause caused some controversy and debate in the React user community, because it could be interpreted to empower Facebook to revoke the license in many scenarios, for example, if Facebook sues the licensee prompting them to take "other action" by publishing the action on a blog or elsewhere. Many expressed concerns that Facebook could unfairly exploit the termination clause or that integrating React into a product might complicate a startup company's future acquisition.[36]

Based on community feedback, Facebook updated the patent grant in April 2015 to be less ambiguous and more permissive:[37]

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.[38]

The Apache Software Foundation considered this licensing arrangement to be incompatible with its licensing policies, as it "passes along risk to downstream consumers of our software imbalanced in favor of the licensor, not the licensee, thereby violating our Apache legal policy of being a universal donor", and "are not a subset of those found in the [Apache License 2.0], and they cannot be sublicensed as [Apache License 2.0]".[39]. In August 2017, Facebook dismissed the Apache Foundation's downstream concerns and refused to reconsider their license[40][41]. The following month, WordPress decided to switch their Gutenberg and Calypso projects away from React.[42]

On September 23, 2017, Facebook announced that the following week, it would re-license Flow, Jest, React, and Immutable.js under a standard MIT License; the company stated that React was "the foundation of a broad ecosystem of open source software for the web", and that they did not want to "hold back forward progress for nontechnical reasons."[43]

On September 26, 2017, React 16.0.0 was released with the MIT license.[44] The MIT license change has also been backported to the 15.x release line with React 15.6.2.[45]

See also

  • AngularJS
  • Angular
  • Vue.js
  • Comparison of JavaScript frameworks
  • React Native

References

1. ^{{cite web|url=https://www.youtube.com/watch?v=GW0rj4sNH2w|last1=Occhino|first1=Tom|last2=Walke|first2=Jordan|title=JS Apps at Facebook|website=YouTube|access-date=22 Oct 2018}}
2. ^{{cite web |url=https://github.com/facebook/react/releases |title=Releases – Facebook/React |website=GitHub}}
3. ^{{Cite web|url= https://reactjs.org|title=React - A JavaScript library for building user interfaces.|website=React|access-date=7 April 2018}}
4. ^{{cite web |url=https://www.infoworld.com/article/2608181/javascript/react--making-faster--smoother-uis-for-data-driven-web-apps.html |title=React: Making faster, smoother UIs for data-driven Web apps |last=Krill |first=Paul |date=May 15, 2014 |website=InfoWorld}}
5. ^{{cite web |url=https://www.infoq.com/news/2013/06/facebook-react |title=Facebook's React JavaScript User Interfaces Library Receives Mixed Reviews |last=Hemel |first=Zef |date=June 3, 2013 |website=InfoQ}}
6. ^{{cite web |url=https://thenewstack.io/javascripts-history-and-how-it-led-to-reactjs/ |title=JavaScript’s History and How it Led To ReactJS |last=Dawson |first=Chris |date=July 25, 2014 |website=The New Stack}}
7. ^{{Cite news|url=https://medium.freecodecamp.org/integrating-create-react-app-redux-react-router-redux-observable-bootstrap-altogether-216db97e89a3|title=How to integrate create-react-app with all the libraries you need to make a great app|last=Dere|first=Mohan|date=2018-02-19|work=freeCodeCamp|access-date=2018-06-14}}
8. ^{{Cite web|url=https://medium.com/about-codecademy/react-router-to-redux-first-router-2fea05c4c2b7|title=React Router to Redux First Router|last=Samp|first=Jon|date=2018-01-13|website=About Codecademy|access-date=2018-06-14}}
9. ^{{cite web|title=React (JS Library): How was the idea to develop React conceived and how many people worked on developing it and implementing it at Facebook?|url=https://www.quora.com/React-JS-Library/How-was-the-idea-to-develop-React-conceived-and-how-many-people-worked-on-developing-it-and-implementing-it-at-Facebook/answer/Bill-Fisher-17|website=Quora}}
10. ^{{cite web|url=https://www.youtube.com/watch?v=A0Kj49z6WdM|title=Pete Hunt at TXJS}}
11. ^{{Cite news|url=https://techcrunch.com/2017/04/18/facebook-announces-react-fiber-a-rewrite-of-its-react-framework/|title=Facebook announces React Fiber, a rewrite of its React framework|publisher=TechCrunch|author=Frederic Lardinois|date=18 April 2017|access-date=19 April 2017}}
12. ^{{cite web|title = React Fiber Architecture|url = https://github.com/acdlite/react-fiber-architecture| website=Github|access-date = 19 April 2017}}
13. ^https://github.com/facebook/react-360/releases
14. ^{{cite web|url=https://reactjs.org/docs/components-and-props.html#props-are-read-only|website=React|title=Components and Props|publisher=Facebook|accessdate=7 April 2018}}
15. ^{{cite web |url=https://reactjs.org/docs/refs-and-the-dom.html |title=Refs and the DOM |website=React Blog}}
16. ^{{cite web|title=Draft: JSX Specification|url=https://facebook.github.io/jsx/|website=JSX|publisher=Facebook|accessdate=7 April 2018}}
17. ^{{cite web |url=https://reactjs.org/blog/2017/09/26/react-v16.0.html#new-render-return-types-fragments-and-strings |title=React v16.0§New render return types: fragments and strings |last=Clark |first=Andrew |date=September 26, 2017 |website=React Blog}}
18. ^{{cite web |url=https://reactjs.org/blog/2017/09/26/react-v16.0.html#support-for-custom-dom-attributes |title=React v16.0§Support for custom DOM attributes |last=Clark |first=Andrew |date=September 26, 2017 |website=React Blog}}
19. ^{{Cite book|url=https://books.google.com/books?id=Tg9QDwAAQBAJ|title=React for Real: Front-End Code, Untangled|last=Fischer|first=Ludovico|date=2017-09-06|publisher=Pragmatic Bookshelf|isbn=9781680504484|language=en}}
20. ^{{cite web|url=https://facebook.github.io/react/blog/2013/06/05/why-react.html|title=Why did we build React? – React Blog|publisher=}}
21. ^{{cite web|title=PayPal Isomorphic React|url=https://www.paypal-engineering.com/2015/04/27/isomorphic-react-apps-with-react-engine/}}
22. ^{{cite web|title=Netflix Isomorphic React|url=http://techblog.netflix.com/2015/01/netflix-likes-react.html}}
23. ^{{cite web|url=https://facebook.github.io/flux/docs/in-depth-overview.html|title=In Depth OverView|publisher=Facebook|accessdate=7 April 2018|website=Flux}}
24. ^{{cite web|last1=Johnson|first1=Nicholas|title=Introduction to Flux - React Exercise|url=http://nicholasjohnson.com/react/course/exercises/flux/|website=Nicholas Johnson|accessdate=7 April 2018}}
25. ^{{cite web|last1=Abramov|first1=Dan|title=The History of React and Flux with Dan Abramov|url=http://threedevsandamaybe.com/the-history-of-react-and-flux-with-dan-abramov/|website=Three Devs and a Maybe|accessdate=7 April 2018}}
26. ^{{cite web|title=State Management Tools - Results|url=https://stateofjs.com/2017/state-management/results|website=The State of JavaScript|accessdate=7 April 2018}}
27. ^{{Cite web|title = Meeting Notes|url = https://discuss.reactjs.org/c/meeting-notes|website = React Discuss|accessdate = 2015-12-13}}
28. ^{{Cite web|title = reactjs/react-future - The Future of React|url = https://github.com/reactjs/react-future|website = GitHub|accessdate = 2015-12-13}}
29. ^{{Cite web|title = facebook/react - Feature request issues|url = https://github.com/facebook/react/labels/Type:%20Feature%20Request|website = GitHub|accessdate = 2015-12-13}}
30. ^{{Cite web|title = facebook/react Projects wiki|url = https://github.com/facebook/react/wiki/Projects|website = GitHub|accessdate = 2015-12-13}}
31. ^{{Cite web|title = facebook/react - CONTRIBUTING.md|url = https://github.com/facebook/react/blob/master/CONTRIBUTING.md#contributor-license-agreement-cla|website = GitHub|accessdate = 2015-12-13}}
32. ^{{Cite web|title = Contributing to Facebook Projects|url = https://code.facebook.com/cla|website = Facebook Code|accessdate = 2015-12-13}}
33. ^https://reactjs.org/docs/faq-internals.html
34. ^https://custom-elements-everywhere.com/
35. ^{{cite web|title=React CHANGELOG.md|url=https://github.com/facebook/react/blob/master/CHANGELOG.md#0120-october-28-2014|website=GitHub}}
36. ^{{cite web|title=A compelling reason not to use ReactJS|first=Austin|last=Liu|url=https://medium.com/bits-and-pixels/a-compelling-reason-not-to-use-reactjs-beac24402f7b|website=Medium}}
37. ^{{cite web|title=Updating Our Open Source Patent Grant|url=https://code.facebook.com/posts/1639473982937255/updating-our-open-source-patent-grant/}}
38. ^{{cite web|title=Additional Grant of Patent Rights Version 2|url=https://github.com/facebook/react/blob/b8ba8c83f318b84e42933f6928f231dc0918f864/PATENTS|website=GitHub}}
39. ^{{Cite web|url=https://www.apache.org/legal/resolved.html|title=ASF Legal Previously Asked Questions|publisher=Apache Software Foundation|language=en|access-date=2017-07-16}}
40. ^{{Cite web|url=https://code.facebook.com/posts/112130496157735/explaining-react-s-license/|title=Explaining React's License|website=Facebook|access-date=2017-08-18|language=en}}
41. ^{{Cite web|url=https://github.com/facebook/react/issues/10191#issuecomment-323486580|title=Consider re-licensing to AL v2.0, as RocksDB has just done|website=Github|language=en|access-date=2017-08-18}}
42. ^{{Cite web|url= https://techcrunch.com/2017/09/15/wordpress-to-ditch-react-library-over-facebook-patent-clause-risk/|title= WordPress to ditch React library over Facebook patent clause risk |website=TechCrunch|language=en|access-date=2017-09-16}}
43. ^{{Cite web|url= https://code.facebook.com/posts/300798627056246/relicensing-react-jest-flow-and-immutable-js/|title= Relicensing React, Jest, Flow, and Immutable.js |website=Facebook Code|language=en|date=2017-09-23}}
44. ^{{cite web |url=https://reactjs.org/blog/2017/09/26/react-v16.0.html#mit-licensed|title= React v16.0§MIT licensed |last=Clark |first=Andrew |date=September 26, 2017 |website=React Blog}}
45. ^{{cite web |url=https://reactjs.org/blog/2017/09/25/react-v15.6.2.html |title=React v15.6.2 |last=Hunzaker |first=Nathan |date=September 25, 2017 |website=React Blog}}

External links

{{Portal|Free and open-source software}}
  • {{Official website}}
{{JS templating |state=autocollapse}}{{Rich Internet applications}}{{Application frameworks}}{{ECMAScript}}{{Facebook navbox}}

6 : 2015 software|Ajax (programming)|Facebook software|JavaScript web frameworks|Rich Internet application frameworks|Software using the BSD license

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/14 1:04:09