关于javascript:为什么代码中使用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语法及对象

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理