最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it necessary to import react in react 16? - Stack Overflow

programmeradmin1浏览0评论

I know since react 17, it is no longer required to import react, but in my currently project, I am using react 16, surpursingly I can still run my web app without importing react in my jsx code, I dont know why exactly

I can see in the html file, there is a link @16/umd/react.development.js, is this the reason why I can run my web app without importing react in jsx file?

I know since react 17, it is no longer required to import react, but in my currently project, I am using react 16, surpursingly I can still run my web app without importing react in my jsx code, I dont know why exactly

I can see in the html file, there is a link https://unpkg/react@16/umd/react.development.js, is this the reason why I can run my web app without importing react in jsx file?

Share Improve this question edited Mar 11 at 7:39 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Mar 10 at 14:47 Sam PorterSam Porter 211 silver badge2 bronze badges 1
  • 1 The "new JSX transform" was also backported to various earlier versions, see e.g. legacy.reactjs./blog/2020/09/22/… – jonrsharpe Commented Mar 10 at 15:27
Add a comment  | 

1 Answer 1

Reset to default 2

Yes, the reason your web app works without explicitly importing React in your JSX files is that React is being loaded globally via the tag in your HTML file. When React is included using a tag like this, it gets attached to the global window object (i.e., window.React), and since Babel (or other compilers) compiles JSX into React.createElement(...), it automatically uses the globally available React instance, even if you haven't imported it explicitly in your JSX files.

This approach is not really recommended to be relied upon, you should explicitly import React in your JSX files if you’re using React 16. However, if you upgrade to React 17+, you can safely omit the import due to automatic JSX.

发布评论

评论列表(0)

  1. 暂无评论