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
React Getting Started createClass Usage Instructions
Publish Date:2025/03/02 Views:50 Category:React
-
Before using React.createClass, lets look at the official explanation ReactClass createClass(object specification) Create a component class with a given specification (the specification is the object specification). This component implement
React Factory Method - Detailed Explanation of createFactory
Publish Date:2025/03/02 Views:88 Category:React
-
React.createFactory returns a function for generating ReactElement of a given type, similar to React.createElement. The type parameter can be an html tag name (for example: "div", "li", etc.) or a ReactClass object.
React Project Management
Publish Date:2025/03/02 Views:123 Category:React
-
In the article "Beginner's Guide to React - Building a React Runtime Environment", we only introduced how to build a React runtime environment, which is actually how to reference the React library. But I think it is still a bit confusing for
React Getting Started - Building a React Operating Environment
Publish Date:2025/03/02 Views:62 Category:React
-
React, a front-end framework developed by Facebook, is very popular. There are two ways to run React, one is to use npm, and the other is not to use npm. These involve react-dom, babel, webpack, etc.
Display element or text on mouse hover in vue
Publish Date:2025/03/01 Views:177 Category:Vue
-
Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following
How to find all elements by class name in Vue
Publish Date:2025/03/01 Views:182 Category:Vue
-
Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in
修复 Java 中的 NoSuchElementException 错误
Publish Date:2023/11/13 Views:178 Category:Java
-
本文介绍 Java 中的 NoSuchElementException 错误。异常是在程序执行期间发生的事件。发生异常时会影响正常的程序流程,导致程序异常终止。本文将讨论 java.util.NoSuchElementException 以及如何在 Java 中
Java 异常 Exception in Thread Main Java.Util.NoSuchElementException: No Line Fo
Publish Date:2023/07/16 Views:487 Category:Java
-
本篇文章介绍如何解决Java中的异常 Exception in thread "main" java.util.NoSuchElementException: No line found。java.util.NoSuchElementException 是运行时未经检查的异常
在 Java 中使用 Scanner 时没有此类元素异常
Publish Date:2023/07/13 Views:82 Category:Java
-
本篇文章将介绍如何在 Java 中使用 Scanner 时解决 NoSuchElementException 错误。在 Java 中使用 Scanner 时没有此类元素异常 Scanner 类用于在 Java 程序中获取用户输入。 它使用多种实用方法,如 next()、