Rendering

1. Introduction to Rendering

Rendering is the process by which React converts your components into DOM elements that are displayed in the browser. Understanding how React handles rendering helps you build efficient and responsive applications. React uses a virtual DOM to optimize the rendering process, making it fast and efficient.

2. The Virtual DOM

The virtual DOM is an in-memory representation of the real DOM elements generated by React components. When the state of a component changes, React updates the virtual DOM and then calculates the most efficient way to update the real DOM to reflect these changes. This process is called reconciliation.

How It Works:

  • React creates a virtual DOM tree from the components' render methods.

  • When a component's state changes, React creates a new virtual DOM tree.

  • React compares the new virtual DOM tree with the previous one and determines the minimal set of changes needed to update the real DOM.

  • React updates the real DOM with these changes.

3. Initial Rendering

The initial rendering of a React application occurs when the ReactDOM.render method is called. This method takes a React component and a DOM element as arguments and renders the component into the DOM element.

Example:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

In this example, the App component is rendered into the DOM element with the id root.

4. Re-Rendering

Re-rendering occurs when the state or props of a component change. React re-renders the component and its children to reflect the new state or props.

Example:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

export default Counter;

In this example, clicking the button updates the state, causing React to re-render the Counter component with the new count.

5. Conditional Rendering

Conditional rendering allows you to render different components or elements based on certain conditions. This can be done using JavaScript conditional statements such as if or ternary operators.

Example:

function Greeting(props) {
  if (props.isLoggedIn) {
    return <h1>Welcome back!</h1>;
  } else {
    return <h1>Please sign up.</h1>;
  }
}

export default Greeting;

In this example, the Greeting component renders different messages based on the value of the isLoggedIn prop.

6. Lists and Keys

Rendering lists of data is a common task in React applications. When rendering lists, it's important to provide a unique key for each list item to help React identify which items have changed, are added, or are removed.

Example:

function TodoList(props) {
  const todos = props.todos;
  const listItems = todos.map((todo) =>
    <li key={todo.id}>{todo.text}</li>
  );

  return (
    <ul>{listItems}</ul>
  );
}

export default TodoList;

In this example, the TodoList component renders a list of todo items. Each li element has a unique key prop to help React efficiently update the list.

7. Fragmentation

React Fragments allow you to group multiple elements without adding an extra node to the DOM. This is useful for returning multiple elements from a component without wrapping them in an unnecessary div.

Example:

import React from 'react';

function FragmentExample() {
  return (
    <React.Fragment>
      <h1>First Element</h1>
      <p>Second Element</p>
    </React.Fragment>
  );
}

export default FragmentExample;

In this example, the FragmentExample component returns a h1 and a p element wrapped in a React.Fragment.

Otherwise, we can write the Fragment with less syntax using just the symbols <>:

import React from 'react';

function FragmentExample() {
  return (
    <>
      <h1>First Element</h1>
      <p>Second Element</p>
    </>
  );
}

export default FragmentExample;

Rendering is a crucial part of building React applications, as it determines how your components are displayed in the browser. Understanding the virtual DOM, initial rendering, re-rendering, conditional rendering, lists and keys, and React Fragments helps you build efficient and responsive applications. By leveraging these concepts, you can create dynamic and interactive user interfaces that are both performant and maintainable.


Help us improve the content 🤩

You can leave comments here.

Last updated