ReactJS

Rendering Elements in ReactJS

Hello ReactJS Developers,

Today we will learn about Rendering Elements in ReactJS.

Before learning about elements in ReactJS, learn the Basics of ReactJS.

Elements in ReactJS

The smallest building blocks of React applications are Elements. Elements are what you want to show or see on the screen.

Example of Element in ReactJS

const helloElement = <h1>Hello, MageComp</h1>;

Rendering an Element into the DOM

<div id=“root”></div>

We call this “root” DOM node because React DOM manages everything inside it.

A React application usually has a single root DOM node. However, while integrating React into an existing app, you can add as many separate root DOM nodes as needed.

For rendering elements in React, you need to pass the DOM element to ReactDOM.createRoot(), then pass the React element to the root.render().

const rootDOM = reactDOM.createRoot(document.getElementById(‘root));
Const helloElement = <h1>Hello, MageComp</h1>;
rootDOM.render(helloElement);

Output:

If you run this from your project, it displays “Hello, MageComp” on the page.

Updating the Rendered Element

React elements are fixed. This means once an element is created, its children or attributes can’t be changed. An element is like a single frame in a movie: it represents the UI at a particular time.

Hence, the only way to update the UI is to create a new element, and pass it to root.render().

Const rootDOM = ReactDOM.createRoot(document.getElementById(‘root));

function callMe()
{
    const helloElement = (
       <div>
           <h1>Hello, MageComp</h1>
           <h2>The Time is {new Date().toLocaleTimeString()}.</h2>
       </div>
    );
    rootDOM.render(helloElement);
}
setInterval(callMe, 1000);

It calls rootDOM.render every second from a setInterval() callback.

React Only Updates What’s Necessary

React DOM compares the element and its children to the previous one and only applies the DOM updates necessary to bring the DOM to the desired state.

Conclusion:

Hence, this was all about rendering elements in ReactJS. Share the tutorial with your ReactJS friends and stay in touch with us for more such guides.

Happy Coding!

Click to rate this post!
[Total: 5 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

6 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago