为什么代码中使用import React from 'react'
文章目录
在我们写代码的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语法及对象

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论