Friday, November 15, 2024
Google search engine
HomeLanguagesReactJS Rendering Elements

ReactJS Rendering Elements

In this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.

What are React Elements?

React elements are different from DOM elements as React elements are simple JavaScript objects and are efficient to create. React elements are the building blocks of any React app and should not be confused with React components which will be discussed in further articles.

Rendering an Element in React

In order to render any element into the Browser DOM, we need to have a container or root DOM element. It is almost a convention to have a div element with the id=”root” or id=”app” to be used as the root DOM element. Let’s suppose our index.html file has the following statement inside it.

<div id="root"></div>

Now, in order to render a simple React Element to the root node, we must write the following in the App.js file.

javascript




import React, { Component } from 'react';
 
class App extends Component {
 
    render() {
        return (
            <div>
                <h1>Welcome to neveropen!</h1>
            </div>
 
        );
    }
}
 
export default App;


Output:

gfg

Now, you have created your first ever React Element and also have rendered it in place, but React was not developed to create static pages, the intention of using React is to create a more logical and active webpage. In order to do so, we will need to update the elements. This next section will guide us through the same.

Updating an Element in React

React Elements are immutable i.e. once an element is created it is impossible to update its children or attribute. Thus, in order to update an element, we must use the render() method several times to update the value over time. Let’s see this as an example. 

Write the following code in App.js file of your project directory.

javascript




import React from 'react';
import ReactDOM from 'react-dom';
 
function App() {
    const myElement = (
        <div>
            <h1>Welcome to neveropen!</h1>
            <h2>{new Date().toLocaleTimeString()}</h2>
        </div>
    );
 
    ReactDOM.render(
        myElement,
        document.getElementById("root")
    );
}
 
setInterval(App, 1000);
 
export default App;


Output:

gfg

In the above example, we have created a function showTime() that displays the current time, and we have set an interval of 1000ms or 1 sec that recalls the function each second thus updating the time in each call. For simplicity, we have only shown the timespan of one second in the given image.

React Render Efficiency

React is chosen over the legacy of DOM updates because of its increased efficiency. React achieves this efficiency by using the virtual DOM and efficient differentiating algorithm.

In the example of displaying the current time, at each second we call the render method, and the virtual DOM gets updated and then the differentiator checks for the particular differences in Browser DOM and the Virtual DOM and then updates only what is required such as in the given example the time is the only thing that is getting changed each time not the title “Welcome to neveropen!” thus React only updates the time itself making it much more efficient than conventional DOM manipulation.

Important Points to Note:

  • Calling the render() method multiple times may serve our purpose for this example, but in general, it is never used instead a stateful component is used which we will cover in further articles.
  • A React Element is almost never used isolated, we can use elements as the building blocks of creating a component in React. Components will also be discussed in upcoming articles.
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments