React.createElement
时间: 2023-10-20 22:03:36 浏览: 106
React.createElement是React中用来创建React元素的方法。它接受三个参数: 类型(type),属性(props),和子元素(children)。
在使用JSX语法时,JSX会被编译为React.createElement()的调用,从而创建React元素。
下面是一个示例代码,展示了如何使用React.createElement创建React元素:
```javascript
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')
);
```
在上述代码中,使用React.createElement创建了三个li元素(child1, child2, child3),然后这些li元素作为数组传入到React.createElement中创建了一个ul元素(root)。最后,使用ReactDOM.render将root渲染到id为'content'的DOM元素中。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [React.createElement方法使用详解](https://blog.csdn.net/lixingshi/article/details/53857397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [leslie1943#blog#React-JSX和React.createElement1](https://download.csdn.net/download/weixin_35804761/86252862)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文