How to modularize code in ReactJS ?
Last Updated :
10 Nov, 2023
Modularize code in React JS refers to dividing it into segments or modules, where each file is responsible for a feature or specific functionality. React code can easily be modularized by using the component structure. The approach is to define each component into different files. With each component separated into different files, all we have to do is figure out how to access the code defined in one file within another file. To access one file into another file, ReactJS provides the functionality to import and export the files.
Module Import and Export in React JS:
It enables us to use code from one file in other locations across our projects, which becomes increasingly important as we build out larger applications.
Export Module:
Exporting a component, or module of code, allows us to call that export in other files, and use the embedded code within other modules.
There are two ways to export code in React:
- Export Default: We can use the export default syntax.
- Named Exports: We can explicitly name our exports.
Export Default:
We can only use export default once per file. The syntax allows us to give any name when we want to import the given module.
Syntax:
export default COMPONENT_NAME
Named Exports:
With named exports, we can export multiple pieces of code from a single file, allowing us to call on them explicitly when we import. And for multiple such exports, we can use a comma to separate two-parameter names within the curly braces.
Syntax:
export {CODE1, CODE2}
Import:
The import keyword enables us to call the modules that we've exported and use them in other files throughout our applications. There are many ways to import the modules in React, and the method that we use depends on how we exported it.
Importing default export:
In order to import the default export from a file, we can use only the address and use the keyword import before it, or we can give any name to the import.
Syntax:
import ANY_NAME from ADDRESS
Importing named exports:
Named export code can be imported by giving the name of that module inside curly braces followed by the address of that file containing that module. For multiple modules, we can use a comma to separate two-parameter names within the curly braces.
Syntax:
import {Code1, Code2} from ADDRESS
Steps to Create React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:

Example: This example demonstrates different exports and imports to implement modularize code.
JavaScript
// Filename: index.js
import React from "react";
import ReactDOM from "react-dom";
// Importing CSS
import "./index.css";
// Importing default export
import File from "./DefaultExport";
// Importing named exports
import { NamedExport } from "./NamedExport";
ReactDOM.render(
<React.StrictMode>
<File />
<NamedExport />
</React.StrictMode>,
document.getElementById("root")
);
JavaScript
// Filename: DefaultExport.js
import React from "react";
const DefaultExport = () => {
return (
<div>
<h1>This is from default export</h1>
<h2>Hello Coders</h2>
</div>
);
};
// Default export
export default DefaultExport;
JavaScript
// Filename: NamedExport.js
import React from "react";
const NamedExport = () => {
return (
<div>
<h1>This is from named export</h1>
<h2>Nice to see you</h2>
</div>
);
};
// Named Export
export { NamedExport };
Steps to Run the Application: Use this command in the terminal inside the project directory.
npm start
Output: This output will be visible on the http://localhost:3000/ on the browser window.

Similar Reads
How to Learn ReactJS in 2025? npx create-react-app myappnpm startnpm run buildnpm installAren't all the above commands familiar to you? If yes, then you might be working on React, or you might have started working on this amazing JavaScript Library. If you're a passionate developer, then surely you might be aware of the populari
10 min read
How to combine multiple reducers in ReactJS ? To combine multiple reducers in React JS we have a function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them. Approach In React, to combine and implement multiple reducers combineReducers methods are used. It manages multiple redu
4 min read
How to dynamically load module in React.js ? In React JS loading of modules statically is cumbersome as it loads the modules beforehand even if it is not rendered. Some components render only when the user interacts, so in such cases, it can lead to more resource consumption. When you statically import modules, you are loading larger data than
3 min read
How to Show or Hide Element in React ? We can show or hide element in React dynamically by accessing the visibility of the elements with the help of state. We can toggle and update the state and render the variable as required.ApproachTo Show or Hide Element in React we will be using the state variable as a boolean value. We will conditi
5 min read
How to export modules with sub-modules in ReactJS ? One of the key features of React is its modular structure, which allows developers to break down their code into reusable components. In many cases, these components are organized into modules and submodules, which can be exported and imported into other parts of the application. Prerequisites:NPM
3 min read
How to Implement Code Splitting in React ? In this article, we will learn about Implementing React code splitting via React.lazy() and Suspense improves app performance by loading code asynchronously and on-demand. What is Code Splitting?Code splitting is a technique used to split a large JavaScript bundle into smaller chunks, which are load
2 min read
How to import components in React Native ? React Native is a framework developed by Facebook for creating native-style applications for Android & iOS under one common language, i.e. JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can
5 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to Work with and Manipulate State in React ? Working with and Manipulating state in React JS makes the components re-render on the UI by Updating the DOM tree. It makes sure to render the latest information and data on the interface. Prerequisites:NPM & Node.jsReact JSState in React JSReact JS Class componentsReact JS Functional Components
6 min read
How to make Reusable React Components ? ReactJS is a JavaScript library used to build single-page applications (SPAs). React makes it easy to create an interactive user interface by using a component-based approach. In this article, we will learn about Reusable React Components with examples. Table of Content What are Reusable React Compo
4 min read