What is React ?

Lasitha Randika
3 min readJan 11, 2021

React is an open-source, front end, JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications.

Notable Features

1. Components

React code is made of entities called components. Components can be rendered to a particular element in the DOM using the React DOM library. When rendering a component, one can pass in values that are known as “props”

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

2. 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. This process is called reconciliation. 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. This selective rendering provides a major performance boost. It saves the effort of recalculating the CSS style, layout for the page and rendering for the entire page.

3. Functional components

Functional components are declared with a function that then returns some JSX.

const FunctionalComponent = (props) => <div>Hello, {props.name}!</div>;

4. Class-based components

Cass-based components are declared using ES6 classes.

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

5. Lifecycle methods

Lifecycle methods 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.
  • 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.

6. JSX

JSX, or JavaScript XML, is an extension to the JavaScript language syntax. 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 called XHP.

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

7. React hooks

Hooks are functions that let developers “hook into” React state and lifecycle features from function components.Hooks don’t work inside classes — they let you use React without classes.

React provides a few built-in hooks like useState, useContext, useReducer and useEffect. Others are documented in the Hooks API Reference.useState and useEffect, which are the most used, are for controlling state and side effects respectively.

Rules of hooks

There are rules of hooks which describe the characteristic code pattern that hooks rely on.

  1. Hooks should only be called at the top level (not inside loops or if statements).
  2. Hooks should only be called from React function components, not normal functions or class components

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, which may call other hooks.

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

--

--