Why Repetitive Code is Problem in React?
Introduction
When it comes to building React applications, developers usually encounter a lot of repetitive code because similar components are used repeatedly. The situation becomes worse when your project is not maintained well, and the possible chances of mistakes are higher. This article will discuss very simple and easy ways to stop repetitive code in React.
What is redundant code?
It means writing similar or identical codes on multiple occasions. Most likely, this happens in making UI components that are alike but differ from one another through headings, descriptions, and images alone.
Why Should You Not Repeat Code?
- Difficult Maintenance: You have to change everything when you need to make a change in some of them.
- Error Prone: More code means more opportunities for mistakes to be made.
- Time-Consuming: Repeatedly typing the same things takes time.
- Slower: More runtime inefficient code can cause a slow application.
- Less Readable: Difficult for newcomers to understand the code base.
Example of Repetitive Code
import React from "react";
import img from "../assets/home.jpg";
function Service() {
return (
<div className="flex justify-evenly">
<div className="p-7">
<div className="flex justify-center p-5">
<div className="w-[60px] h-[60px] bg-gray-500 rounded-full p-2">
<img src={img} className="w-full h-full object-cover rounded-full" alt="" />
</div>
</div>
<div>
<h3 className="text-center font-bold">FREE AND FAST DELIVERY</h3>
<p className="text-center text-[12px]">Free delivery for all orders over $140</p>
</div>
</div>
<div className="p-7">
<div className="flex justify-center p-5">
<div className="w-[60px] h-[60px] bg-gray-500 rounded-full p-2">
<img src={img} className="w-full h-full object-cover rounded-full" alt="" />
</div>
</div>
<div>
<h3 className="text-center font-bold">24/7 CUSTOMER SERVICE</h3>
<p className="text-center text-[12px]">Friendly 24/7 customer support</p>
</div>
</div>
<div className="p-7">
<div className="flex justify-center p-5">
<div className="w-[60px] h-[60px] bg-gray-500 rounded-full p-2">
<img src={img} className="w-full h-full object-cover rounded-full" alt="" />
</div>
</div>
<div>
<h3 className="text-center font-bold">MONEY BACK GUARANTEE</h3>
<p className="text-center text-[12px]">We return money within 30 days</p>
</div>
</div>
</div>
);
}
export default Service;
What Is Wrong with This Code?
- Replication: The structure of each of the services is the same.
- Hard to Update: If you want to change any design. You need to do that three times.
- Scalability Problems: The more services, the more duplicate code.
How to Cure Repetitive Code in React
To solve this problem, we can create a reusable React component. Here's how:
Step 1: Create a Reusable Component in React
import React from "react"; import img from "../assets/home.jpg"; function Service({ heading, description }) { return ( <div className="p-7"> <div className="flex justify-center p-5"> <div className="w-[60px] h-[60px] bg-gray-500 rounded-full p-2"> <img src={img} className="w-full h-full object-cover rounded-full" alt="" /> </div> </div> <div> <h3 className="text-center font-bold">{heading}</h3> <p className="text-center text-[12px]">{description}</p> </div> </div> ); } export default Service;
Step 2: Create an Object to Store Data
const services = {
"FREE AND FAST DELIVERY": "Free delivery for all orders over $140",
"24/7 CUSTOMER SERVICE": "Friendly 24/7 customer support",
"MONEY BACK GUARANTEE": "We return money within 30 days"
};
Step 3: Render Components Using map()
in React
import React from "react";
import Service from "./Service";
function ServiceList() {
return (
<div className="flex justify-evenly">
{Object.keys(services).map((key) => (
<Service key={key} heading={key} description={services[key]} />
))}
</div>
);
}
export default ServiceList;
Advantages of approach
- With less code, you don't need to repeat the same structure.
- Changes the design in place.
- Flexibility: More services can be added with no extra code.
- It can grow: it can do large jobs easily.
- Better Readability: Neat coding is easy to read and maintain.
Smart Strategies to Reduce Repetitive Code
- Higher-Order Components (HOCs): Wrapping components to augment functionality.
- Custom Hooks: For reuse of logic.
- Compound Components: For partial sharing of state among components.
- Render Props: To determine at runtime what is to be re-rendered.
- Context API: State to be shared globally without prop drilling.
- Reusable Utility Functions: For logic that is used repeatedly, outside components.
Practical Example in React Applications
Suppose you are building an e-commerce website using React. The chances are that you will design product cards that display the product name, price, and product image.
Why is my React code running twice?
- Hardcoded Data: Always data should remain out of the component structure.
- Ignoring Reusability: When copy-paste happens more than thrice, it's time to refactor.
- Overcomplicating: Components are to be kept simple and concern Papam with one task.
- No Keys in Lists: That might lead to problems during the rendering.
- Overstate: Not everything needs to go into state.
- Performance Optimization: With React.memo, lazy loading, and code-splitting, their use would be required.
0 Comments
If you have any doubts. Please let me know