The Wayback Machine - https://web.archive.org/web/20240905220345/https://www.geeksforgeeks.org/reactjs-rendering-elements/
Open In App

ReactJS Rendering Elements

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 GeeksforGeeks!</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 GeeksforGeeks!</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 GeeksforGeeks!” 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.


Previous Article
Next Article

Similar Reads

Server Side Rendering vs Client Side Rendering vs Server Side Generation
In the world of web development, there are several approaches to rendering web pages: server-side rendering, client-side rendering, and server-side generation. Each approach has its own advantages and disadvantages, and choosing the right one for your project depends on your specific needs and goals. In this blog, we’ll explore the differences betw
4 min read
Explain lifecycle of component re-rendering due to re-rendering of parent component
React is a javascript library that renders components written in JSX. You can build and render any component as per your usage. States can be updated accordingly using the setState method. Props can be updated only by the parent component. Updating of state and props leads to the rendering of UI. Different lifecycle methods are called during these
2 min read
How does SSR(Server-Side Rendering) differ from CSR(client-side rendering) ?
Server-Side Rendering (SSR) and Client-Side Rendering (CSR) are two different approaches used in web development to render web pages to users. Each approach has its own set of advantages and disadvantages. In this article, we will learn about the difference between SSR and CSR with examples and features. Table of Content Server-Side Rendering (SSR)
4 min read
How to set focus on an input field after rendering in ReactJS ?
Setting focus on an input field after rendering in ReactJS can be done in the following ways, but before doing that, set up your project structure with the following steps: Creating React Application: Step 1: Create a React application using the following command: npx create-react-app foldernameStep 2: After creating your project folder i.e. folder
2 min read
How to prevent page from rendering before redirect in ReactJS ?
ReactJS is a popular front-end framework that allows developers to create dynamic and responsive web applications. One common issue that developers face when working with ReactJS is preventing a page from rendering before a redirect is complete. This can lead to issues such as slow loading times, broken links, and a poor user experience. In this ar
4 min read
Conditional rendering component using Enums in ReactJS
In certain scenarios, a ReactJS developer may have to dynamically display or hide components depending on specific conditions. For instance, when creating a To-Do list application, the developer must render tasks if there are pending items; otherwise, they should display a message such as "No pending tasks available." Prerequisites:NodeJS or NPMRea
3 min read
What is typical pattern for rendering a list of components from an array of data in ReactJS ?
The pattern for rendering a list of components from an array of data can be done by mapping all individual custom pieces of data to the component. With the map function, we will map every element data of the array to the custom components in a single line of code.  Prerequisites:NodeJS or NPMReact JSSteps to Create the React Application:Step 1: Cre
2 min read
Re-rendering Components in ReactJS
Re-rendering components in ReactJS is a part of the React Component Lifecycle and is called by React only. React components automatically re-render whenever there is a change in their state or props and provide dynamic content in accordance with user interactions. Re-render mainly occurs if there is Update in StateUpdate in propRe-rendering of the
4 min read
How do React Portals help in rendering elements outside the root DOM node?
React Portals offer a powerful mechanism for rendering elements outside of the typical DOM hierarchy in React applications. They provide a way to render content at a different place in the DOM tree from where it is defined in the component hierarchy. This capability opens up various possibilities, such as modals, tooltips, and overlays, that requir
6 min read
CSS shape-rendering Property
The shape-rendering property is used to hint the renderer about the tradeoffs that have to be made while rendering shapes like circles, rectangles or paths. The renderer can be told to make the shape geometrically precise or optimize the shape to speed up rendering in certain situations. Syntax: shape-rendering: auto | optimizeSpeed | crispEdges |
4 min read
CSS image-rendering Property
The image-rendering property is used to set the type of algorithm used for image scaling. This property can be used to modify the scaling behavior when the user scales the image above or below the original dimensions. Syntax: shape-rendering: auto | crisp-edges | pixelated | initial | inherit Property Values: auto: It is used to indicate that the s
3 min read
Node.js Server Side Rendering (SSR) using EJS
Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situations where the client has a slow internet connec
3 min read
Template Rendering via Marko.js in Node.js
Marko: Marko makes it easy to represent your UI using a syntax that is like HTML. It is a friendly and super fast UI library that makes building web apps bearable and fun. It is so much like HTML, that you can use it as a replacement for a templating language like handlebars, mustache, or pug. However, Marko is much more than a templating language.
3 min read
SVG text-rendering Attribute
The text-rendering attribute gives hint about what contracts should be made while rendering text. It has an effect only on the text element. Syntax: text-renderring = auto | optimizeLegibility | geometricPrecision | optimizeSpeed Attribute Values: The text-rendering attribute accepts the values mentioned above and described below: auto: It shows th
2 min read
SVG shape-rendering Attribute
The shape-rendering attribute hints the renderer about the tradeoff's to be made while rendering shapes like paths, circles, or rectangles. It has effect only on the following elements: &lt;circle&gt;, &lt;ellipse&gt;, &lt;line&gt;, &lt;path&gt;, &lt;polygon&gt;, &lt;polyline&gt;, and &lt;rect&gt;. Syntax: shape-rendering = auto | optimizeSpeed | c
2 min read
CSS text-rendering
Text-rendering is a property that allows you to choose what to optimize when rendering text. It sends the information to the rendering engine about what to optimize for while rendering text. It's actually not a CSS property and is not defined in any CSS standard. It's an SVG property but Gecko and Webkit browsers allow you to apply this property to
2 min read
Vue.js Declarative Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Declarative rendering in Vue enabl
2 min read
Vue.js Conditional Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Conditional Rendering in Vue makes
2 min read
p5.js DOM and Rendering Complete Reference
DOM is a way to represent the webpage in a structured hierarchical way so that it will become easier for programmers and users to glide through the document. The functions used in p5.js are mentioned below. DOM Description select()It searchs an element in the page with the given id, class or tag name and return it as a p5.element. selectAll()It sea
2 min read
How to position absolute rendering the button in a new line ?
The position CSS property sets how an element is positioned in a document. Absolute positioning allows you to place your element where you want. The positioning is done relative to the first absolutely positioned element. The element we position absolute will be removed from the normal flow of the document and no space is created in the page for th
2 min read
Critical Rendering Path Flow
Have you ever wondered what occurs behind the scenes when webpage loads or how your browser processes these pages? Then you've come to the right place to discover more about it. The flow that a browser follows to transform a file into a webpage is known as the Critical Rending Path. The below diagram shows the flow of the Critical Rendering Path(CR
4 min read
Flask Rendering Templates
Flask is a backend web framework based on the Python programming language. It basically allows the creation of web applications in a Pythonic syntax and concepts. With Flask, we can use Python libraries and tools in our web applications. Using Flask we can set up a web server to load up some basic HTML templates along with Jinja2 templating syntax.
13 min read
jQWidgets jqxScheduler rendering Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxScheduler widget is used to show a set of appointments in a day, week, month, timeline day, timeline week as well as timeline month views. The rendering prope
2 min read
Vue.js List Rendering v-for with a Range
Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. The Vue JS is supported by all popular browsers like Chrome, Firefox, IE, Safari, etc. There are a lot of data that needs to
3 min read
Vue.js List Rendering v-for with v-if
Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the for loop. There are a lot of data that needs to be r
3 min read
Vue.js List Rendering v-for with an Object
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. In order to repeat a task for a fi
3 min read
Vue.js List Rendering v-for on a &lt;template&gt;
Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the for loop. There are a lot of data that needs to be r
3 min read
Vue.js List Rendering v-for with a Component
Vue.js is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any developer. In order to repeat a task for a fixed amount of time, we make use of the for loop. The Components are used to build the combination of UI elements that need to be called any number of times
3 min read
Vue.js List Rendering Mutation Methods
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. In order to repeat a task for a fi
3 min read
jQWidgets jqxDataTable rendering Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxDataTable is used for reading and displaying the data from the HTML Table. This is also used to display data from various data sources like XML, JSON, Array,
3 min read