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
1 Answer
Reset to default 2Yes, 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.