What is the use of React.createElement ?
Last Updated :
02 Nov, 2023
React.createElement is a fundamental method of React JS. The main use of React.createElement is the Creation of a React component. It is the JavaScript format for creating react components. Also, the JSX react component when transpired invokes this only method for creating the component.
Syntax:
React.createElement(type,{props},children);
// JSX code
<type {props} >{children}</type
Parameters: React.createElement() takes three arguments. They are:
- type: the type of the HTML element (h1,p, button, etc).
- props: properties of the object({style:{size:10px}} or Eventhandlers, classNames,etc).
- children: anything that needs to be enclosed by that component.
React DOM: React DOM contains the arguments that are necessary to render react elements in the browser.
ReactDOM.render(element,containerElement);
Parameters: ReactDOM.render() takes two arguments:
- element: The element that needs to be rendered in the DOM.
- containerElement: Where to render in the dom.
Steps to create React application
Step 1: Create a react application using the below command:
npx create-react-app foldername
Step 2: Once your folder is created, change your directory to the newly created folder using the below-mentioned command.
cd foldername
Project Structure:
Project StructureExample: This code in Index.js shows the use of react.createEement method and render that component using ReactDOM.render method.
JavaScript
// Filename - index.js
import React from 'react';
import ReactDOM from 'react-dom';
let demo = React.createElement(
"h1", { style: { color: "green" } }, "Welcome to GeeksforGeeks"
)
ReactDOM.render(
demo,
document.getElementById('root')
);
Step to run the application: Run your application using the following command in the terminal.
npm start
Output: This output will be visible on http://localhost:3000
Similar Reads
What is the use of Fragment in React ? Fragment in React is used to combine the child nodes and render without creating an extra parent Node. The Fragment Node is not rendered on the DOM instead it just groups up the elements/ child nodes. In ReactJS, we render the JSX from the component with the help of a return statement that can only
3 min read
What is the use of data-reactid attribute in HTML ? The data-reactid attribute is a custom attribute that react can easily identify its components within the DOM. Just like the HTML "classes" and "id" attributes, "data-reactid" helps in uniquely identifying the element.What is data-reactid?The data-reactid is an attribute that is used as a unique ide
2 min read
What are the features of ReactJS ? Created by Facebook, ReactJS is a JavaScript library designed for crafting dynamic and interactive applications, elevating UI/UX for web and mobile platforms. Operating as an open-source, component-based front-end library, React is dedicated to UI design and streamlines code debugging by employing a
4 min read
What are the advantages of using JSX in ReactJS ? JavaScript XML or JSX is an extension of the JavaScript language syntax. The React library is an extension to write XML-like code for elements and components. JSX tags have a tag name, attributes, and children. Although JSX is not a necessity to write React applications, it is extremely beneficial
2 min read
What are the advantages of React.js ? React.js is a popular JavaScript library used for building dynamic and interactive user interfaces. It has become a go-to tool for developers due to its efficient architecture, high performance, and ease of use. This article highlights the key advantages of using React.js for web development and exp
5 min read