React.createElement() Method
React.createElement() Method is used to create elements. Whenever we write code in JSX, JSX converts it to React.createElement(). The createElement method is not recommended to use as it is very hard to maintain or debug. We’ve to call the React.createElement() method every time for the creation of a React element, even if it is just a span tag with no attributes.
Syntax:
React.createElement(
type,
[props],
[...children]
)
Example: In this example, we have created a div element using React.createElement() method.
Javascript
import React from 'react' ; import "./styles.css" ; const title = React.createElement( 'h1' , { className: 'title' }, 'w3wiki' ); const App = () => React.createElement( 'div' , {}, [ React.createElement( 'button' , { className: 'btn' }, title), React.createElement( 'button' , { className: 'btn' }, title), ]); export default App; |
Output:
What is the difference between createElement and cloneElement ?
This article will help us to gain knowledge of createElement and cloneElement and we will also learn them with code examples and their difference.
We will learn the following methods:
Table of Content
- React.createElement()
- React.cloneElement()
- Difference between createElement and cloneElement