Open main menu

Wikipedia β

React (JavaScript library)

In computing, React (sometimes styled React.js or ReactJS) is a JavaScript library[2] for building user interfaces.

React
React-icon.svg
Original author(s) Jordan Walke, Denis Popov
Developer(s) Facebook, Instagram and community
Initial release March 2013; 4 years ago (2013-03)
Stable release
16.0.0 / September 26, 2017; 23 days ago (2017-09-26)[1]
Repository github.com/facebook/react
Development status Active
Written in JavaScript
Platform Cross-platform
Size 109 KiB production
710 KiB development
Type JavaScript library
License MIT
Website reactjs.org

It is maintained by Facebook, Instagram and a community of individual developers and corporations.[3][4][5]

React allows developers to create large web-applications that use data and can change over time without reloading the page. It aims primarily to provide speed, simplicity, and scalability. React processes only user interfaces in applications. This corresponds to View in the Model-View-Controller (MVC) pattern, and can be used in combination with other JavaScript libraries or frameworks in MVC, such as AngularJS.[6]

Contents

HistoryEdit

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

Basic usageEdit

The following is a rudimentary example of how React can be used in html using JSX and the ECMAScript 2015 JavaScript syntax.

<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 featuresEdit

One-way data flowEdit

To use React to its greatest potential, Properties (or props), ideally a set of immutable values, are passed to a component's render function. A component should not directly modify any properties passed to it, but should be passed callback functions that instead modifies the store creating a single source of truth. This mechanism's promise is expressed as "properties flow down; actions flow up". The described mechanism is an architecture called Flux.[11][12] Many Flux alternatives have been created since its inception however the community has moved toward Redux.[13]

Virtual DOMEdit

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

JSXEdit

React components are typically written in JSX, a JavaScript extension syntax allowing quoting of HTML and using HTML tag syntax to render subcomponents.[15] This is a React-specific grammar extension to JavaScript like the now-defunct E4X. HTML syntax is processed into JavaScript calls of the React framework. Developers may also write in pure JavaScript. JSX is similar to another extension syntax created by Facebook for PHP, XHP. JSX looks like regular HTML. An example of JSX code:

import React from 'react';

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

export default App;
Nested elements

Multiple elements need to be wrapped in a single container element like the <div> element shown above. As of React v16.0 it is now possible to return an array of elements, fragments and strings.[16]

Attributes

Custom attributes are supported in addition to HTML attributes. The custom attributes need to be added with the data- prefix. Custom attributes as of v16.0 are now passed through to the DOM.[17]

JavaScript expressions

JavaScript expressions can be used inside JSX with curly brackets {}:

import React from 'react';

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>{10+1}</h1>
      </div>
    );
  }
}

export default App;

The example above will render

<div>
  <h1>11</h1>
</div>
Ternary operators

If–else statements cannot be used inside JSX but ternary expressions can be used instead. The example below will render { i === 1 ? 'true' : 'false' } as the string 'true' because i is equal to 1.

import React from 'react';

class App extends React.Component {
  render() {
    var i = 1;
    return (
      <div>
        <h1>{ i === 1 ? 'true' : 'false' }</h1>
      </div>
    );
  }
}

export default App;

Not only can you return strings via ternary operators you can also pass functions and fragments of code.

import React from 'react';

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

export default App;

The example above if arr has values (of which it has 3) will render:

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

Architecture beyond HTMLEdit

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]

React NativeEdit

React Native libraries were announced by Facebook in 2015,[21] providing the React architecture to native Android, iOS, and UWP[22] applications.

Future developmentEdit

Project status can be tracked via the core team discussion forum.[23] However major changes to React go through the Future of React repo, Issues and PR.[24][25] This enables the React community to provide feedback on new potential features, experimental APIs and JavaScript syntax improvements.

Sub projectsEdit

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

Facebook CLAEdit

Facebook requires contributors to React to sign the Facebook CLA.[27][28]

Licensing controversyEdit

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

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

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

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

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

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

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

ReferencesEdit

  1. ^ "Releases – Facebook/React". GitHub. 
  2. ^ "A JavaScript library for building user interfaces - React". facebook.github.io. Retrieved 2017-04-13. 
  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. ^ "Angular vs React: Feature Comparison of JS Tools". ThinkMobiles. February 22, 2017. 
  7. ^ "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. 
  8. ^ "Pete Hunt at TXJS". 
  9. ^ Frederic Lardinois (18 April 2017). "Facebook announces React Fiber, a rewrite of its React framework". TechCrunch. Retrieved 19 April 2017. 
  10. ^ "React Fiber Architecture". Github. Retrieved 19 April 2017. 
  11. ^ https://facebook.github.io/flux/docs/in-depth-overview.html#content
  12. ^ https://medium.com/@cabot_solutions/flux-the-react-js-application-architecture-a-comprehensive-study-fd2585d06483
  13. ^ https://stateofjs.com/2016/statemanagement/
  14. ^ "Refs and the DOM". React Blog. 
  15. ^ "JSX in Depth". Retrieved 2015-11-17. 
  16. ^ Clark, Andrew (September 26, 2017). "React v16.0§New render return types: fragments and strings". React Blog. 
  17. ^ Clark, Andrew (September 26, 2017). "React v16.0§Support for custom DOM attributes". React Blog. 
  18. ^ "Why did we build React? – React Blog". 
  19. ^ "PayPal Isomorphic React". 
  20. ^ "Netflix Isomorphic React". 
  21. ^ "React Native: Bringing modern web techniques to mobile". 
  22. ^ Windows Apps Team (April 13, 2016). "React Native on the Universal Windows Platform". blogs.windows.com. Retrieved 2016-11-06. 
  23. ^ "Meeting Notes". React Discuss. Retrieved 2015-12-13. 
  24. ^ "reactjs/react-future - The Future of React". GitHub. Retrieved 2015-12-13. 
  25. ^ "facebook/react - Feature request issues". GitHub. Retrieved 2015-12-13. 
  26. ^ "facebook/react Projects wiki". GitHub. Retrieved 2015-12-13. 
  27. ^ "facebook/react - CONTRIBUTING.md". GitHub. Retrieved 2015-12-13. 
  28. ^ "Contributing to Facebook Projects". Facebook Code. Retrieved 2015-12-13. 
  29. ^ "React CHANGELOG.md". GitHub. 
  30. ^ Liu, Austin. "A compelling reason not to use ReactJS". Medium. 
  31. ^ "Updating Our Open Source Patent Grant". 
  32. ^ "Additional Grant of Patent Rights Version 2". GitHub. 
  33. ^ "ASF Legal Previously Asked Questions". Apache Software Foundation. Retrieved 2017-07-16. 
  34. ^ "Explaining React's License". Facebook. Retrieved 2017-08-18. 
  35. ^ "Consider re-licensing to AL v2.0, as RocksDB has just done". Github. Retrieved 2017-08-18. 
  36. ^ "WordPress to ditch React library over Facebook patent clause risk". TechCrunch. Retrieved 2017-09-16. 
  37. ^ "Relicensing React, Jest, Flow, and Immutable.js". Facebook Code. 2017-09-23. 
  38. ^ Clark, Andrew (September 26, 2017). "React v16.0§MIT licensed". React Blog. 
  39. ^ Hunzaker, Nathan (September 25, 2017). "React v15.6.2". React Blog. 

External linksEdit