Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to optimize React component to render it ?
Next article icon

How to make Reusable React Components ?

Last Updated : 22 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Components ?
  • How to make Reusable React Components ?

What are Reusable React Components ?

Reusable React Components are pieces of code that can be shared and reused across different files in your application. It is a modular piece of UI that takes data from props to build complex and interactive user interfaces. It encapsulates the login that can be reused again.

Features of reusable components:

  • It can accept props data to customize according to data.
  • It can be used to encapsulate the same logic and reuse it again.
  • It helps us to break the code into smaller units that can be reused again.
  • It makes it easy to maintain the application.

How to make Reusable React Components ?

When creating reusable React components, it's important to keep in mind two key factors:

1. Avoid side Effects:

It is not recommended to include logic that interacts with external data, such as making API calls, directly inside a reusable component. Instead, you should pass this logic as props to the component.

For example, if a button not only has a visual function but also fetches data from the internet, it may not be reusable in other contexts.

In this case, we have a reusable button component that lacks best practices. In the example section, I will demonstrate why this is the case.

// It is a Reusable ButtonReusable component and this is a bad practices
const ButtonReusable = () => {
return (
<button> Click Me </button>
);
}

2. Use Props:

Props are parameters passed to a component to customize its behavior and appearance, making it reusable for different purposes.

// It is a buttonReusable component that can change its color
const ButtonReusable = ({ color }) => {
return (
<button style={{ backgroundColor: blue }}> Click Here </button>
);
}

This is considered bad practice because the label on the button is fixed as "Click Here". If you wish to change the text on your button to, for example, "Sign Up", you would have to go back to the button component and make that change. This means that every time you want to use a different text, you would have to edit the code again. In other words, the button would no longer be reusable.

Steps to Create a React Application:

Step 1: Create a React application using the following command:

npx create-react-app gfg

Step 2: After creating your project folder(i.e. gfg), move to it by using the following command:

cd gfg

Example: This below example demonstrate the Reusable React Components.

In this example, You can see ProductList and ProductItem component can be composed together and accept props for customization and are reusable with different data sets.

JavaScript
//File path: src/App.js import React from 'react'; import ProductList from './ProductList.js';  const App = () => {     const products = [         { id: 1, name: 'Product 1', price: 10 },         { id: 2, name: 'Product 2', price: 20 },         { id: 3, name: 'Product 3', price: 30 },     ];      return (         <div style={{ margin: '5px' }}>             <h2 style={{ color: 'green' }}>                 GeeksForGeeks | Reusable Components Example             </h2>             <ProductList products={products} />         </div>     ); };  export default App; 
JavaScript
//File path: src/ProductList.js import React from 'react'; import ProductItem from './ProductItem.js';  const ProductList = ({ products }) => {     return (         <div>             <h2>Products List</h2>             <table border={1}>                 <thead>                     <tr>                         <th>No</th>                         <th>Name</th>                         <th>Price</th>                     </tr>                 </thead>                 <tbody>                     {products.map((product) => (                         <ProductItem key={product.id}                             product={product} />                     ))}                 </tbody>             </table>         </div>     ); };  export default ProductList; 
JavaScript
//File path: src/ProductItem.js import React from 'react';  const ProductItem = ({ product }) => {     return (         <tr>             <td>{product.id}</td>             <td>{product.name}</td>             <td>{product.price}</td>         </tr>     ); };  export default ProductItem; 

To run the application use the following command:

npm run start 

Output: Now go to http://localhost:3000 in your browser

reusable-component



Next Article
How to optimize React component to render it ?
author
jaimin78
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ReactJS-Component

Similar Reads

  • How to Integrate Redux with React Components ?
    Redux is an open-source JavaScript library for managing and centralizing application state. It helps you to write applications that behave consistently and are easy to test and run in different environments. It can also be understood as the predictable state container for the JavaScript app. It is m
    4 min read
  • How to optimize React component to render it ?
    ReactJS mainly depends upon the props (which are passed to it) and the state of the Component. Hence to reduce the number of times Component renders we can reduce the props and state it depends upon. This can be easily done by separating the logic of one component into several individual child compo
    3 min read
  • React Rebass Image Component
    React Rebass is a front-end framework that was designed keeping react in mind. In this article, we will know how to use Image Component in React Rebass. The Image is an important component that is required in each development. So to create an Image component, we can import the Rebass Image component
    2 min read
  • How to use HOCs to reuse Component Logic in React ?
    In React, making reusable components keeps your code neat. Higher-order components (HOCs) are a smart way to bundle and reuse component logic. HOCs are like magic functions that take a component and give you back an upgraded version with extra powers or information. HOCs can be implemented in a few
    4 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
  • React Native Smart Components
    In this article, we are going to learn about Smart components in React Native. The smart component is one of the categories of React Components so before diving into the smart components detail. A Component is one of the core building blocks of React and React-Native. The component is a block of cod
    3 min read
  • How to use Paper Component in ReactJS ?
    In Material Design, the physical properties of paper are translated to the screen. Material UI for React has this component available for us and it is very easy to integrate. We can use the Paper Component in ReactJS using the following approach. Prerequisites:React JSMaterial UIApproachTo use Paper
    2 min read
  • How To Use Modal Component In ReactJS?
    Modals are a popular UI component used to display content in a pop-up window like alerts, forms, or additional information. In ReactJS, there are multiple ways to implement a modal. In this article, we will discuss two common approaches: using reusable functional components and using the Material-UI
    4 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
  • React styled-components Module
    React Styled-component Module allows us to write CSS within JavaScript in a very modular and reusable way in React. Instead of having one global CSS file for a React project, we can use styled-component to enhance the developer experience. It also removes the mapping between components and styles –
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences