React_(web_framework)

React (JavaScript library)

React (JavaScript library)

JavaScript library for building user interfaces


React (also known as React.js or ReactJS) is a free and open-source front-end JavaScript library[3][4] for building user interfaces based on components. It is maintained by Meta (formerly Facebook) and a community of individual developers and companies.[5][6][7]

Quick Facts Original author(s), Developer(s) ...

React can be used to develop single-page, mobile, or server-rendered applications with frameworks like Next.js. Because React is only concerned with the user interface and rendering components to the DOM, React applications often rely on libraries for routing and other client-side functionality.[8][9] A key advantage of React is that it only rerenders those parts of the page that have changed, avoiding unnecessary rerendering of unchanged DOM elements.

Notable features

Declarative

React adheres to the declarative programming paradigm.[10]:76 Developers design views for each state of an application, and React updates and renders components when data changes. This is in contrast with imperative programming.[11]

Components

React code is made of entities called components.[10]:10–12 These components are modular and reusable.[10]:70 React applications typically consist of many layers of components. The components are rendered to a root element in the DOM using the React DOM library. When rendering a component, values are passed between components through props (short for "properties"). Values internal to a component are called its state.[12]

The two primary ways of declaring components in React are through function components and class components.[10]:118[13]:10

Function components

Function components are declared with a function (using JavaScript function syntax or an arrow function expression) that accepts a single "props" argument and returns JSX. From React v16.8 onwards, function components can use state with the useState Hook.

React Hooks

On February 16, 2019, React 16.8 was released to the public, introducing React Hooks.[14] Hooks are functions that let developers "hook into" React state and lifecycle features from function components.[15] Notably, Hooks do not work inside classes — they let developers use more features of React without classes.[16]

React provides several built-in hooks such as useState,[17][13]:37 useContext,[10]:11[18][13]:12 useReducer,[10]:92[18][13]:65–66 useMemo[10]:154[18][13]:162 and useEffect.[19][13]:93–95 Others are documented in the Hooks API Reference.[20][10]:62 useState and useEffect, which are the most commonly used, are for controlling state[10]:37 and side effects,[10]:61 respectively.

Rules of hooks

There are two rules of hooks[21] which describe the characteristic code patterns that hooks rely on:

  1. "Only call hooks at the top level" — don't call hooks from inside loops, conditions, or nested statements so that the hooks are called in the same order each render.
  2. "Only call hooks from React functions" — don't call hooks from plain JavaScript functions so that stateful logic stays with the component.

Although these rules can't be enforced at runtime, code analysis tools such as linters can be configured to detect many mistakes during development. The rules apply to both usage of Hooks and the implementation of custom Hooks,[22] which may call other Hooks.

Server components

React server components or "RSC"s[23] are function components that run exclusively on the server. The concept was first introduced in the talk Data Fetching with Server Components Though a similar concept to Server Side Rendering, RSCs do not send corresponding JavaScript to the client as no hydration occurs. As a result, they have no access to hooks. However, they may be asynchronous function, allowing them to directly perform asynchronous operations:

async function MyComponent() {
  const message = await fetchMessageFromDb();

  return (
    <div>Message: {message}</div>
  );
}

Currently, server components are most readily usable with Next.js.

Class components

Class components are declared using ES6 classes. They behave the same way that function components do, but instead of using Hooks to manage state and lifecycle events, they use the lifecycle methods on the React.Component base class.

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

The introduction of React Hooks with React 16.8 in February 2019 allowed developers to manage state and lifecycle behaviors within functional components, reducing the reliance on class components.

This trend aligns with the broader industry movement towards functional programming and modular design. As React continues to evolve, it's essential for developers to consider the benefits of functional components and React Hooks when building new applications or refactoring existing ones.[24]

Routing

React itself does not come with built-in support for routing. React is primarily a library for building user interfaces, and it doesn't include a full-fledged routing solution out of the box. Third-party libraries can be used to handle routing in React applications.[25] It allows the developer to define routes, manage navigation, and handle URL changes in a React-friendly way.

There is a Virtual DOM that is used to implement the real DOM

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.[26] This process is called reconciliation. This allows the programmer to write code as if the entire page is rendered on each change, while React only renders the components that actually change. This selective rendering provides a major performance boost.[27]

Updates

When ReactDOM.render[28] is called again for the same component and target, React represents the new UI state in the Virtual DOM and determines which parts (if any) of the living DOM needs to change.[29]

The virtualDOM will update the realDOM in real-time effortlessly

Lifecycle methods

Lifecycle methods for class-based components use a form of hooking that allows the execution of code at set points during a 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.
  • componentDidUpdate is invoked immediately after updating occurs.[30]
  • componentWillUnmount is called immediately before the component is torn 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, which should be reflected in the user interface.

JSX

JSX, or JavaScript Syntax Extension, is an extension to the JavaScript language syntax.[31] Similar in appearance to HTML,[10]:11 JSX provides a way to structure component rendering using syntax familiar[10]:15 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 called XHP.

An example of JSX code:

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

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,[32] and Netflix and PayPal use universal loading to render identical HTML on both the server and client.[33][34]

Server-side rendering

Server-side rendering (SSR) refers to the process of rendering a client-side JavaScript application on the server, rather than in the browser. This can improve the performance of the application, especially for users on slower connections or devices.

With SSR, the initial HTML that is sent to the client includes the fully rendered UI of the application. This allows the client's browser to display the UI immediately, rather than having to wait for the JavaScript to download and execute before rendering the UI.

React supports SSR, which allows developers to render React components on the server and send the resulting HTML to the client. This can be useful for improving the performance of the application, as well as for search engine optimization purposes.

Common idioms

React does not attempt to provide a complete application library. 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.

Unidirectional data flow

To support React's concept of unidirectional data flow (which might be contrasted with AngularJS's bidirectional flow), the Flux architecture was developed as 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.[35] When used with React, this propagation is accomplished through component properties. Since its conception, Flux has been superseded by libraries such as Redux and MobX.[36]

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

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

In February 2019, useReducer was introduced as a React hook in the 16.8 release. It provides an API that is consistent with Redux, enabling developers to create Redux-like stores that are local to component states.[40]

Future development

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

History

React was created by Jordan Walke, a software engineer at Meta, who initially developed a prototype called "F-Bolt"[44] before later renaming it to "FaxJS". This early version is documented in Jordan Walke's GitHub repository. Influences for the project included XHP, an HTML component library for PHP.

React was first deployed on Facebook's News Feed in 2011 and subsequently integrated into Instagram in 2012 [citation needed]. In May 2013, at JSConf US, the project was officially open-sourced, marking a significant turning point in its adoption and growth.

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

On April 18, 2017, Facebook announced React Fiber, a new set of internal algorithms for rendering, as opposed to React's old rendering algorithm, Stack.[45] React Fiber was to become the foundation of any future improvements and feature development of the React library.[46][needs update] The actual syntax for programming with React does not change; only the way that the syntax is executed has changed.[47] React's old rendering system, Stack, was developed at a time when the focus of the system on dynamic change was not understood. Stack was slow to draw complex animation, for example, trying to accomplish all of it in one chunk. Fiber breaks down animation into segments that can be spread out over multiple frames. Likewise, the structure of a page can be broken into segments that may be maintained and updated separately. JavaScript functions and virtual DOM objects are called "fibers", and each can be operated and updated separately, allowing for smoother on-screen rendering.[48]

On September 26, 2017, React 16.0 was released to the public.[49]

On August 10, 2020, the React team announced the first release candidate for React v17.0, notable as the first major release without major changes to the React developer-facing API.[50]

On March 29, 2022, React 18 was released which introduced a new concurrent renderer, automatic batching and support for server side rendering with Suspense.[51]

More information Version, Release Date ...

Licensing

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

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

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

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

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

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

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

See also


References

  1. Occhino, Tom; Walke, Jordan. "JS Apps at Facebook". YouTube. Retrieved 22 Oct 2018.
  2. "18.2.0 (June 14, 2022)". Retrieved 23 June 2022.
  3. "React - A JavaScript library for building user interfaces". reactjs.org. Archived from the original on April 8, 2018. Retrieved 7 April 2018.
  4. "Chapter 1. What Is React? - What React Is and Why It Matters [Book]". www.oreilly.com. Archived from the original on May 6, 2023. Retrieved 2023-05-06.
  5. Krill, Paul (May 15, 2014). "React: Making faster, smoother UIs for data-driven Web apps". InfoWorld. Retrieved 2021-02-23.
  6. Hemel, Zef (June 3, 2013). "Facebook's React JavaScript User Interfaces Library Receives Mixed Reviews". infoq.com. Archived from the original on May 26, 2022. Retrieved 2022-01-11.
  7. Dawson, Chris (July 25, 2014). "JavaScript's History and How it Led To ReactJS". The New Stack. Archived from the original on Aug 6, 2020. Retrieved 2020-07-19.
  8. "Components and Props". React. Facebook. Archived from the original on 7 April 2018. Retrieved 7 April 2018.
  9. "Introducing Hooks". react.js. Retrieved 2019-05-20.
  10. "Hooks at a Glance – React". reactjs.org. Retrieved 2019-08-08.
  11. "What the Heck is React Hooks?". Soshace. 2020-01-16. Retrieved 2020-01-24.
  12. "Using the State Hook – React". reactjs.org. Retrieved 2020-01-24.
  13. "Using the State Hook – React". reactjs.org. Retrieved 2020-01-24.
  14. "Using the Effect Hook – React". reactjs.org. Retrieved 2020-01-24.
  15. "Hooks API Reference – React". reactjs.org. Retrieved 2020-01-24.
  16. "Rules of Hooks – React". reactjs.org. Retrieved 2020-01-24.
  17. "Building Your Own Hooks – React". reactjs.org. Retrieved 2020-01-24.
  18. Chourasia, Rawnak (2023-03-08). "Convert Class Component to Function(Arrow) Component - React". Code Part Time. Retrieved 2023-08-15.
  19. "Mastering React Router - The Ultimate Guide". 2023-07-12. Retrieved 2023-07-26.
  20. "Refs and the DOM". React Blog. Retrieved 2021-07-19.
  21. "React: The Virtual DOM". Codecademy. Retrieved 2021-10-14.
  22. "ReactDOM – React". reactjs.org. Retrieved 2023-01-08.
  23. "Reconciliation – React". reactjs.org. Retrieved 2023-01-08.
  24. "React.Component – React". legacy.reactjs.org. Retrieved 2024-04-09.
  25. "Draft: JSX Specification". JSX. Facebook. 2022-03-08. Retrieved 7 April 2018.
  26. Hunt, Pete (2013-06-05). "Why did we build React? – React Blog". reactjs.org. Retrieved 2022-02-17.
  27. "PayPal Isomorphic React". medium.com. 2015-04-27. Archived from the original on 2019-02-08. Retrieved 2019-02-08.
  28. "Netflix Isomorphic React". netflixtechblog.com. 2015-01-28. Retrieved 2022-02-14.
  29. "In Depth OverView". Flux. Facebook. Retrieved 7 April 2018.
  30. "Flux Release 4.0". Github. Retrieved 26 February 2021.
  31. Johnson, Nicholas. "Introduction to Flux - React Exercise". Nicholas Johnson. Retrieved 7 April 2018.
  32. Abramov, Dan. "The History of React and Flux with Dan Abramov". Three Devs and a Maybe. Retrieved 7 April 2018.
  33. "State Management Tools - Results". The State of JavaScript. Retrieved 29 October 2021.
  34. "Meeting Notes". React Discuss. Retrieved 2015-12-13.
  35. "facebook/react - Feature request issues". GitHub. Retrieved 2015-12-13.
  36. "React Fiber Architecture". Github. Retrieved 19 April 2017.
  37. "React v16.0". react.js. 2017-09-26. Retrieved 2019-05-20.
  38. "React v18.0". reactjs.org. Retrieved 2022-04-12.
  39. "ASF Legal Previously Asked Questions". Apache Software Foundation. Retrieved 2017-07-16.
  40. "Explaining React's License". Facebook. Retrieved 2017-08-18.
  41. Clark, Andrew (September 26, 2017). "React v16.0§MIT licensed". React Blog.
  42. Hunzaker, Nathan (September 25, 2017). "React v15.6.2". React Blog.

Bibliography


Share this article:

This article uses material from the Wikipedia article React_(web_framework), and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.