![6eef181b66ccdc209515225fc2d189f3.png](https://i-blog.csdnimg.cn/blog_migrate/319aaa73fe305c98caae897d9ed36c83.jpeg)
概述
本文主要学习React.createElement()API的应用。在学习之前,我们需要知道React的几个基础核心概念,React Element,React Component,React Instance不熟悉的童鞋推荐阅读下文。
React Components, Elements, and Instances。
Babel与JSX
我们都知道JSX是React中的一种标签语法,它是一个语法糖,主要用来取代React.createElement()产生React Element。但是浏览器呢不支持JSX,因此我们需要一个工具Babel来进行翻译。因此Babel就是把JSX转为浏览器相兼容的格式。
![1a6cb457ed76d14035efd7295d0534cb.png](https://i-blog.csdnimg.cn/blog_migrate/4d20c2217869d99843f480cdef809b9b.png)
我们可以去Babel官网提供的环境写几个DEMO看看它是怎么转换的。
![39290d0e33c5e68dee7d26db37a8b328.png](https://i-blog.csdnimg.cn/blog_migrate/702b686f5a26e68b656f385df8d13339.jpeg)
![e1da85f433046be8c89309879b5a4476.png](https://i-blog.csdnimg.cn/blog_migrate/2f5ac4168bbe2be0c6cc1e841e6c6eb2.jpeg)
![083adc5f59b3ace293bf7beb51243777.png](https://i-blog.csdnimg.cn/blog_migrate/5f7877cf5c42a65f56a2580ffb9d34dd.jpeg)
React.createElement
源码位置在createelement。
// ReactSymbols.js
demo
我们将下面代码在React中执行看看打印出来的React Element是什么样子。
function
![c29c4f45fd700813ea8e3a571e7d59d9.png](https://i-blog.csdnimg.cn/blog_migrate/e567e99634c67583c533264b0a917b46.jpeg)
我们可以对比下真正打出的React Element。
![b562d726d60d2286871689b3ff9c8c6b.png](https://i-blog.csdnimg.cn/blog_migrate/c8782e583dd1e4d3309a0259adde887f.jpeg)
现在对React.createElemt有一个粗浅的认识,至于如何将其转为为真正的DOM往后看。
源码目录见
https://github.com/NuoHui/react-source-codegithub.com