'Does import React from "react" store the entire react library in the React object or is the React object 1 of many objects in the react library?
I have been studying React and I am trying to understand conceptually what the import statement that I see at the top of every React.js file import React from "react" is actually doing. I have been reading through the React docs https://reactjs.org/docs/react-api.html but not sure if I am understanding the concept correctly.
Does the import statement
import React from "react"mean that I am importing the entire react library into the .js file and storing the entire react library inside a new object I am naming "React"?Or is the "React" object an object that is already defined inside the react library and I am using the import statement to pull the "React" object out of the react library to use some of its methods? In other words the react library contains other objects as well as the React object from my import statement and my imports could be something like this
import React from "react"
import someOtherObject from "react"
Solution 1:[1]
As you import a module from your JavaScript file, the module will be cached, for example by Node.js or a browser. Importing the same module from multiple JavaScript files won't create a new object each time.
Although you can use React APIs like React.useState, destructured named imports (ex. import { useState } from "react") is the preferred style going into the future according to this post from React Blog.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alanhchoi |
