React.createElement method usage details
When you see the createElement method, you will naturally think of document.createElement(). Yes, the names of these two methods are the same. However, here we are going to introduce React.createElement() instead of document.createElement().
Let’s get straight to the point
React.createElement
First, let’s look at the official explanation.
ReactElement createElement(
string/ReactClass type,
[object props],
[children ...]
)
The React.createElement method creates and returns a ReactElement element of a given type. The type parameter can be a string of an html tag name or a ReactClasss. The type parameter is required for createElement. The second parameter is the attributes of the tag, which is optional. The third parameter is the child nodes of the element, which is also optional.
Below we will briefly introduce the first parameter type.
The type parameter is the HTML tag name
The type parameter can be an HTML tag name or a ReactClass. First, let's look at an example using HTML tags. This example comes from the official website, and all the following examples are rewritten around this example.
Example 1
var child1 = React.createElement('li', null, 'First Text Content');
var child2 = React.createElement('li', null, 'Second Text Content');
var child3 = React.createElement('li', null, 'Third Text Content');
var root = React.createElement('ul', { className: 'my-list' }, child1, child2, child3);
ReactDOM.render(
root,
document.getElementById('content')
);
This example is very simple. The first parameter is the name of the HTML tag ul and li. Let's look at the second and third parameters of the first three createElements. Because the second parameter is not needed above, but the third parameter is needed as the text content of li, that is, the sub-content of li, so the second parameter is assigned null.
But for the createElement method that creates the ul element, the first parameter is also the HTML tag name ul, and the second parameter is a props object { className : 'my-list'}. Of course, we can also add other properties here, such as { className : 'my-list', name :'ulname'}. These properties can be called through this.props.name. But the strange thing is that there are multiple parameters after the second parameter. How to explain this? Is it that createElement has more than three parameters? From the second parameter onwards, there are as many parameters as there are child nodes of the node. You can think so, but if we slightly modify the above example, we can see that there is actually nothing wrong with saying that createElement has three parameters.
Example 2
var child1 = React.createElement('li', null, 'First Text Content');
var child2 = React.createElement('li', null, 'Second Text Content');
var child3 = React.createElement('li', null, 'Third Text Content');
var root = React.createElement('ul', { className: 'my-list' }, [child1, child2, child3]);
ReactDOM.render(
root,
document.getElementById('content')
);
We put all the parameters after the second parameter in an array, so that we can think of the third parameter as an array, and the elements in the array are all the child nodes of the node.
From this we can see that the use of React is actually very flexible.
The type parameter is ReactClass
In the example above, the type parameter is the HTML tag name. The usage is actually quite simple, but you need to pay attention to the second and third parameters. Next, let's look at the usage of type ReactClass.
Example 3
var cli = React.createClass({
render:function(){
return (
<li>
{this.props.text}
</li>
)
}
})
var child1 = React.createElement(cli, {key:'F',text:'First Text Content'});
var child2 = React.createElement(cli, {key:'S',text:'Second Text Content'});
var child3 = React.createElement(cli, {key:'T',text:'Third Text Content'});
var root = React.createElement('ul', { className: 'my-list' }, [child1, child2, child3]);
ReactDOM.render(
root,
document.getElementById('content')
);
Here we see that the first parameter cli is the return value of createClass. It should be noted that for the first three li, the second parameter of createElement must add key:'value'. The values here are different from each other. If this attribute is not specified - although it can be displayed normally according to the logic - the following warning will be reported
Warning: Each child in an array or iterator should have a unique "key" prop. Check the top-level render call using <ul>. See https://fb.me/react-warning-keys for more information.
Of course, if we only create one li element in the above example, with only child1 and no child2 or child3, then the key attribute of the second parameter must also be specified, otherwise the above warning will also be reported.
For Example 3, we can also rewrite it as follows
Example 4
var cli = React.createClass({
render:function(){
return (
<li>
{this.props.children}
</li>
)
}
})
var child1 = React.createElement(cli, {key:'F'},'First Text Content');
var child2 = React.createElement(cli, {key:'S'},'Second Text Content');
var child3 = React.createElement(cli, {key:'T'},'Third Text Content');
var root = React.createElement('ul', { className: 'my-list' }, [child1, child2, child3]);
ReactDOM.render(
root,
document.getElementById('content')
);
Similarly, specify the third parameter in createElement and reference it in createClass using this.props.children.
The above is the usage of React.createElement. It can be seen that its usage is very flexible. In actual production, we should choose a method that suits us for convenient and quick development.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
MongoDB Adding Elements to an Array
Publish Date:2025/04/11 Views:136 Category:MongoDB
-
This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values to an array The $push operator is one of the various array update operators provided by MongoDB
How to Fix Elementor Not Loading Error (5 Methods)
Publish Date:2025/03/17 Views:54 Category:NETWORK
-
With Elementor, we can replace the block editor with a more intuitive page builder. If you are an Elementor user, you need to load the editor every time you want to create or edit a page. However, sometimes errors can prevent Elementor from loading.
How to avoid cross-origin (CORS) issues in React/Next.js
Publish Date:2025/03/17 Views:170 Category:NETWORK
-
In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:188 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:187 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:102 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:172 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:153 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:60 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code: