Why do you need to import React in functional components ?
Last Updated :
30 Oct, 2023
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It’s ‘V’ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. In react, we use JSX, especially in the functional important which is an extension of Javascript it needs the react methods for reference to convert into javascript code.
Prerequisites Why do we Import React in functional components? The Browser does not understand React, it only understands HTML, CSS, and JavaScript. So to convert React into valid JavaScript we use a webpack called Babel. It is used to convert JSX into objects and then return that object. Babel is already included in the boilerplate generated by create-react-app.
Let's understand with the help of an example.
Creating React Application Step 1: Use this command to create the react project
npx create-react-app reactapp Step 2: After creating your project folder, move to it using the following command:
cd reactapp Project Structure It will look like the following.
Project Structure Step 3: Write the following code in App.js . Below is a JSX syntax. So whenever you use it you will require React imported.
JavaScript
import React from 'react'
import ReactDOM from 'react-dom'
function App () {
return < h1 > Hello , World !< /h1>
}
const rootElement = document . getElementById ( 'root' )
ReactDOM . render ( < App /> , rootElement )
The above JSX gets converted to this by Babel internally
Object $$typeof: Symbol(react.element) key: null props: {children: 'Hello, World!'} ref: null type: "h1" _owner: null _store: {validated: false} _self: null _source: {fileName: '/src/index.js', lineNumber: 5, columnNumber: 10} [[Prototype]]: Object Explanation:
type: In this example, our component is the object that represents the root element which is the h1 element. type is the property that corresponds to the HTML element. props: This property contains all of the children and in our case, it is Hello, World! key: You might know that we pass key as a prop while mapping in React which in our case is null. It is used to uniquely identify an element in the virtual DOM. ref: when you need to imperatively modify a child outside of the typical dataflow we need ref. $$typeof: This property identifies the object as a React element What Browser sees after the code gets compiled is the following code.
Example: It shows how the app component is compiled into an object in the console screen.
JavaScript
import React from 'react'
import ReactDOM from 'react-dom'
function App () {
return < h1 > Hello , World !< /h1>
}
const rootElement = document . getElementById ( 'root' )
console . log ( App ())
ReactDOM . render ( < App /> , rootElement )
Step to run the application: Open the terminal and type the following command.
npm start Output:
Explanations: The JSX gets internally into many React.createElement() function calls and each of them returns an object as shown above. Now because of this, we need to import React from "react" since internally every JSX is creating a React Component using JSX transformer.
Note: From React version 17 you don't even need to import React from "react" for smaller projects but earlier versions of React projects need to import it. JSX transformer internally takes care of converting a JSX to React elements but you may still need to import it for using hooks like useState and useEffect. We do not need to import React in files like actions, reducers, and so on because we don't have JSX there.
Similar Reads
How to create Functional Components in React ?
To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
How to use PropTypes in a Functional Component in React?
PropTypes is a mechanism provided by React for type-checking props passed to components. It allows developers to specify the expected types of props (such as strings, numbers, arrays, objects, etc.) and whether they are required or optional. PropTypes helps catch bugs early by providing warnings in
2 min read
How to create a Functional Component in React?
To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
What is a pure functional component in ReactJS ?
What is a JavaScript function? A JavaScript function is a block of code designed to perform a particular task which is executed when it is called. How React functional components are similar to JavaScript functions? Conceptually, components are like JavaScript functions. Â A functional component is a
3 min read
State in React Functional Component
State in React functional components refers to a way of managing data that influences the rendering and behavior of components. Unlike class components, where state is managed using a this.state object, functional components use the React Hook useState to manage state efficiently.State is Local to t
3 min read
How to use useMemo Hook in a Functional Component in React ?
In React development, optimizing performance is crucial for delivering responsive and fast user experiences. One way to achieve this is by using the `useMemo` hook. In this article, we'll explore what `useMemo` is, why it's beneficial, and how to use it effectively in functional components. Table of
3 min read
Implementing Lazy Loading for Functional Components in React
React is a popular library, maintained by Meta, which is used for building user interfaces and dynamic websites. It offers several techniques to optimize performance and enhance end-user experience. Lazy loading is one such technique that can be used to improve the performance of web applications by
2 min read
What does it mean for a component to be mounted in ReactJS ?
For a component to be mounted in ReactJS it means to be initialized and inserted in the DOM. Mounting is the initial phase in which the instance of the component is created and inserted into the DOM. When the component gets successfully inserted into the DOM, the component is said to be mounted. In
2 min read
ReactJS Functional Components
In ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
Role of Hooks in Functional Components.
Hooks in React are like new tools that make building components easier. Before, we had to use classes, which were like following strict instructions. But with hooks, we can do the same thing more simply and flexibly directly inside functional components. It's like having a magic connector for buildi
3 min read