美文网首页
为什么代码中使用import React from 'react

为什么代码中使用import React from 'react

作者: may505 | 来源:发表于2020-06-29 21:17 被阅读0次

在我们写代码的react组件时,不管是无状态组件还是有状态组件,我们在头部都要引入react,然而在代码中又没有使用到react

import React from 'react';

那是因为我们在代码中写了jsx语法,bable在把jsx语法转换成js语法的时候要用到React.createElement()把jsx对象转换成js语法

const Eel = (
<div>
  <h1>hello world</h1>  
</div>
)
// 会转化成
React.createElement("div", null,React.createElement("h1", null, "hello world"));

在转换的时候会用到React.createElement()方法,所以我们要在头部引入react,只有该js文件写了jsx语法及对象

相关文章

网友评论

      本文标题:为什么代码中使用import React from 'react

      本文链接:https://www.haomeiwen.com/subject/jjgafktx.html