React (JavaScript library)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
React
React-icon.svg
Original author(s) Jordan Walke
Developer(s) Facebook and community
Initial release March 2013; 5 years ago (2013-03)
Stable release
16.5.0 / September 6, 2018; 18 days ago (2018-09-06)[1]
Repository Edit this at Wikidata
Written in JavaScript
Platform Cross-platform
Size 109 KiB production
710 KiB development
Type JavaScript library
License MIT License
Website reactjs.org

In computing, React (also known as React.js or ReactJS) is a JavaScript library[2] for building user interfaces. It is maintained by Facebook and a community of individual developers and companies.[3][4][5]

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

History[edit]

React was created by Jordan Walke, a software engineer at Facebook. He was influenced by XHP, an HTML component framework for PHP.[8] It was first deployed on Facebook's newsfeed in 2011 and later on Instagram.com in 2012.[9] 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.[10] React Fiber was to become the foundation of any future improvements and feature development of the React framework.[11][needs update]

Basic usage[edit]

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

<div id="myReactApp"></div>

<script type="text/babel">
  class Greeter extends React.Component { 
    render() { 
      return <h1>{this.props.greeting}</h1>
    } 
  } 

  ReactDOM.render(<Greeter greeting="Hello World!" />, document.getElementById('myReactApp'));
</script>

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

<div id="myReactApp">
  <h1>Hello World!</h1>
</div>

Notable features[edit]

One-way data binding with props[edit]

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

Stateful components[edit]

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

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

Virtual DOM[edit]

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.[13] This allows the programmer to write code as if the entire page is rendered on each change, while the React libraries only render sub components that actually change.

Lifecycle methods[edit]

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.
  • 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[edit]

JavaScript XML (JSX) is an extension to the JavaScript language syntax[14]. 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 (
      <div>
        <p>Header</p>
        <p>Content</p>
        <p>Footer</p>
      </div>
    );
  }
}
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[15].

Attributes

JSX provides a range of element attributes designed to mirror those provided by HTML. Custom attributes can also be passed to the component[16]. 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 {}:

  <h1>{10+1}</h1>

The example above will render

  <h1>11</h1>
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 (
      <div>
        <h1>{ i === 1 ? 'true' : 'false' }</h1>
      </div>
    );
  }
}

Functions and JSX can be used in conditionals:

class App extends React.Component {
  render() {
    const sections = [1, 2, 3];
    return (
      <div>
        { 
          sections.length > 0
            ? sections.map(n => <div>Section {n}</div>)
            : null
        }
      </div>
    );
  }
}

The above will render:

<div>
  <div>Section 1</div>
  <div>Section 2</div>
  <div>Section 3</div>
</div>

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

Architecture beyond HTML[edit]

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

Common idioms[edit]

React does not attempt to provide a complete 'application framework'. It is designed specifically for building user interfaces[2] 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[edit]

To support React's concept of unidirectional data flow (which might be contrasted with Angular'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[21]. When used with React, this propagation is accomplished through component properties.

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

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[23]. 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.[24]

React Native[edit]

React Native was announced by Facebook in 2015,[25] applying the React architecture to native Android[26], iOS, and UWP[27] applications.

History[edit]

In 2012 Mark Zuckerberg commented, "The biggest mistake we made as a company was betting too much on HTML5 as opposed to native".[28] He promised that Facebook would soon deliver a better mobile experience.

Inside Facebook, Jordan Walke found a way to generate iOS UI elements from a background JavaScript thread. They decided to organize an internal hackathon to perfect this prototype in order to be able to build native apps with this technology.[29]

After a few months of development, Facebook released the first version for the React.js Conf 2015. During a technical talk,[30] Christopher Chedeau explained that Facebook was already using React Native in production for their Group App and their Ads Manager App.[25]

Working principles[edit]

The working principles of React Native are basically the same as React except that it is not manipulating the DOM via the VirtualDom but some native views. It runs in a background process (which interprets the JavaScript written by the developers) directly on the end-device and communicates with the native platform via a serializable, asynchronous and batched Bridge[31].[32]

It can be seen that Facebook corrected the error that Mark Zuckerberg mentioned in 2012:[original research?] React Native doesn't rely on HTML5 at all, everything is written in JavaScript, and relies on native SDKs.

Hello World[edit]

A Hello, World program in React Native looks like this:

 1 import React, { Component } from 'react';
 2 import { AppRegistry, Text } from 'react-native';
 3 
 4 export default class HelloWorldApp extends Component {
 5   render() {
 6     return (
 7       <Text>Hello world!</Text>
 8     );
 9   }
10 }
11 
12 // Skip this line if using Create React Native App
13 AppRegistry.registerComponent('HelloWorld', () => HelloWorldApp);
14 
15 // The ReactJS code can also be imported into another component with the following code:
16 
17 import HelloWorldApp from './HelloWorldApp';

Future development[edit]

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

Sub projects[edit]

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

Facebook CLA[edit]

Facebook requires contributors to React to sign the Facebook CLA.[37][38]

Criticism of ReactJS[edit]

A criticism of ReactJS 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."[39]

Licensing controversy[edit]

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:[40]

"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.[41]

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

"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."[43]

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]."[44]. In August 2017, Facebook dismissed the Apache Foundation's downstream concerns and refused to reconsider their license[45][46]. The following month, WordPress decided to switch their Gutenberg and Calypso projects away from React.[47]

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."[48]

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

See also[edit]

References[edit]

  1. ^ "Releases – Facebook/React". GitHub. 
  2. ^ a b "React - A JavaScript library for building user interfaces". React. Retrieved 7 April 2018. 
  3. ^ Krill, Paul (May 15, 2014). "React: Making faster, smoother UIs for data-driven Web apps". InfoWorld. 
  4. ^ Hemel, Zef (June 3, 2013). "Facebook's React JavaScript User Interfaces Library Receives Mixed Reviews". InfoQ. 
  5. ^ Dawson, Chris (July 25, 2014). "JavaScript's History and How it Led To ReactJS". The New Stack. 
  6. ^ Dere, Mohan (2018-02-19). "How to integrate create-react-app with all the libraries you need to make a great app". freeCodeCamp. Retrieved 2018-06-14. 
  7. ^ Samp, Jon (2018-01-13). "React Router to Redux First Router". About Codecademy. Retrieved 2018-06-14. 
  8. ^ "React (JS Library): How was the idea to develop React conceived and how many people worked on developing it and implementing it at Facebook?". Quora. 
  9. ^ "Pete Hunt at TXJS". 
  10. ^ Frederic Lardinois (18 April 2017). "Facebook announces React Fiber, a rewrite of its React framework". TechCrunch. Retrieved 19 April 2017. 
  11. ^ "React Fiber Architecture". Github. Retrieved 19 April 2017. 
  12. ^ "Components and Props". React. Facebook. Retrieved 7 April 2018. 
  13. ^ "Refs and the DOM". React Blog. 
  14. ^ "Draft: JSX Specification". JSX. Facebook. Retrieved 7 April 2018. 
  15. ^ Clark, Andrew (September 26, 2017). "React v16.0§New render return types: fragments and strings". React Blog. 
  16. ^ Clark, Andrew (September 26, 2017). "React v16.0§Support for custom DOM attributes". React Blog. 
  17. ^ Fischer, Ludovico (2017-09-06). React for Real: Front-End Code, Untangled. Pragmatic Bookshelf. ISBN 9781680504484. 
  18. ^ "Why did we build React? – React Blog". 
  19. ^ "PayPal Isomorphic React". 
  20. ^ "Netflix Isomorphic React". 
  21. ^ "In Depth OverView". Flux. Facebook. Retrieved 7 April 2018. 
  22. ^ Johnson, Nicholas. "Introduction to Flux - React Exercise". Nicholas Johnson. Retrieved 7 April 2018. 
  23. ^ Abramov, Dan. "The History of React and Flux with Dan Abramov". Three Devs and a Maybe. Retrieved 7 April 2018. 
  24. ^ "State Management Tools - Results". The State of JavaScript. Retrieved 7 April 2018. 
  25. ^ a b "React Native: Bringing modern web techniques to mobile". 
  26. ^ "Android Release for React Native". 
  27. ^ Windows Apps Team (April 13, 2016). "React Native on the Universal Windows Platform". blogs.windows.com. Retrieved 2016-11-06. 
  28. ^ "Zuckerberg's Biggest Mistake? 'Betting on HTML5'". Mashable. Retrieved 7 April 2018. 
  29. ^ "A short Story about React Native". Retrieved 16 January 2018. 
  30. ^ Christopher, Chedeau. "A Deep Dive into React Native". YouTube. Retrieved 16 January 2018. 
  31. ^ "Bridging in React Native". 14 October 2015. Retrieved 16 January 2018. 
  32. ^ "React Native Framework".  Thursday, 13 September 2018
  33. ^ "Meeting Notes". React Discuss. Retrieved 2015-12-13. 
  34. ^ "reactjs/react-future - The Future of React". GitHub. Retrieved 2015-12-13. 
  35. ^ "facebook/react - Feature request issues". GitHub. Retrieved 2015-12-13. 
  36. ^ "facebook/react Projects wiki". GitHub. Retrieved 2015-12-13. 
  37. ^ "facebook/react - CONTRIBUTING.md". GitHub. Retrieved 2015-12-13. 
  38. ^ "Contributing to Facebook Projects". Facebook Code. Retrieved 2015-12-13. 
  39. ^ https://reactjs.org/docs/faq-internals.html
  40. ^ "React CHANGELOG.md". GitHub. 
  41. ^ Liu, Austin. "A compelling reason not to use ReactJS". Medium. 
  42. ^ "Updating Our Open Source Patent Grant". 
  43. ^ "Additional Grant of Patent Rights Version 2". GitHub. 
  44. ^ "ASF Legal Previously Asked Questions". Apache Software Foundation. Retrieved 2017-07-16. 
  45. ^ "Explaining React's License". Facebook. Retrieved 2017-08-18. 
  46. ^ "Consider re-licensing to AL v2.0, as RocksDB has just done". Github. Retrieved 2017-08-18. 
  47. ^ "WordPress to ditch React library over Facebook patent clause risk". TechCrunch. Retrieved 2017-09-16. 
  48. ^ "Relicensing React, Jest, Flow, and Immutable.js". Facebook Code. 2017-09-23. 
  49. ^ Clark, Andrew (September 26, 2017). "React v16.0§MIT licensed". React Blog. 
  50. ^ Hunzaker, Nathan (September 25, 2017). "React v15.6.2". React Blog. 

[1]

External links[edit]

  1. ^ , "Fragments in ReactJS(v.16.2.0)", The fragment is a first-class component to wrap your child components and HTML elements instead of using any HTML element - Cronj Blog,