import React from 'react';
When using React in a JavaScript file, we need to import the React library. The import statement above imports the React object from the ‘react’ package.
Here’s a breakdown of the import statement:
- import: This is a keyword used to import code from other files or packages.
- React: This is the variable to which we assign the imported object. We can choose any name for this variable, but it is common to use ‘React’.
- from ‘react’: This specifies the package from which we are importing the code. In this case, we are importing from the ‘react’ package.
By importing React, we gain access to the core features of React, such as creating React components and using JSX syntax.
Example:
import React from 'react';
function App() {
return (
<div>
<h1>Hello, React!</h1>
<p>This is a React component.</p>
</div>
);
}
export default App;
In the above example, we import React at the beginning of the file. Then, we define a functional component named ‘App’. It returns JSX code enclosed in a div, which represents the root element of the component’s output. This component can then be exported and used in other parts of the application.
Note: The ‘export default’ statement at the end is used to make the ‘App’ component available for importing in other files.
+