Back to articles
5 Worst Practices in React and how you can avoid them
Today, I want to talk about these absolutely random 5 worst code practices in React. While it's easy to write code that works, it's much harder to write code that is maintainable, scalable, and efficient. So, let's dive into some examples of what not to do in React.
1) Not using proper component organization
One of the worst practices in React is not using proper component organization. It's important to group your components in a way that makes sense, both for your project structure and for the purpose of the components. For example, if you have a feature that requires multiple components, it's best to create a folder for that feature and put all related components in that folder. Here's an example:
src/
├── App.js
├── components/
│ ├── Header.js
│ └── Footer.js
└── features/
└── dashboard/
├── Dashboard.js
├── Widget.js
└── Chart.js
This structure makes it easy to navigate your codebase and find the components you need. On the other hand, if you have a lot of components in one folder, it can be difficult to find the right one.
2) Not using state and props correctly
Another worst code practice in React is not using state and props correctly. It's important to understand the difference between the two and how to use them effectively.
State is used for data that changes within a component, while props are used for data that is passed down from a parent component. For example, let's say you have a component that needs to display a list of items.
You might use state to store the list of items and props to pass down a function to handle item clicks. Here's an example:
function List(props) {
const [items, setItems] = useState([]);
function handleClick(item) {
props.onItemClick(item);
}
return (
<ul>
{items.map(item => (
<li key={item.id} onClick={() => handleClick(item)}>
{item.name}
</li>
))}
</ul>
);
}
By using state and props correctly, you can make your components more reusable and easier to maintain.
3) Not using React hooks correctly
React hooks are a powerful feature that were introduced in React 16.8. They allow you to use state and other React features in functional components, which makes them much more powerful. However, if you don't use hooks correctly, they can cause a lot of problems.
One of the worst code practices in React is not understanding the rules of hooks. For example, you can't use hooks conditionally, and you can't use them in loops or nested functions. Here's an example of what not to do:
function MyComponent(props) {
let data;
if (props.condition) {
data = useState([]);
} else {
data = useState({});
}
return <div>{data}</div>;
}
This code will cause a "Rules of Hooks" error, because the useState hook is being used conditionally. To fix this, you can use separate state variables for each case, like this:
function MyComponent(props) {
const [listData, setListData] = useState([]);
const [objectData, setObjectData] = useState({});
let data = props.condition ? listData : objectData;
return <div>{data}</div>;
}
4) Not handling errors correctly
Errors are a fact of life in programming, and React is no exception. However, if you don't handle errors correctly, they can cause a lot of problems for your users. One of the worst code practices in React is not handling errors correctly. When an error occurs in your code, React will typically display an error message in the console. However, this doesn't do anything for your users, who may be left with a broken app. To handle errors correctly, you should use a try-catch block and display a meaningful error message to your users. Here's an example:
function MyComponent() {
const [data, setData] = useState(null);
useEffect(() => {
try {
const result = fetchData();
setData(result);
} catch (error) {
setData('Error loading data');
}
}, []);
return <div>{data}</div>;
}
In this example, we're using a try-catch block to catch any errors that occur when fetching data. If an error occurs, we display a meaningful error message to our users.
5) Not optimizing performance
Finally, one of the worst code practices in React is not optimizing performance. React is designed to be fast, but if you're not careful, your app can become slow and unresponsive. To optimize performance, you should use tools like React.memo and useMemo to memoize expensive computations and prevent unnecessary re-renders.
For example, let's say you have a component that displays a list of items, and you're using a map function to render each item. If the list is large, this map function could be expensive and cause your app to slow down. To optimize performance, you can use React.memo to memoize the list component:
const MemoizedList = React.memo(List);
function MyComponent() {
const [items, setItems] = useState([]);
return <MemoizedList items={items} />;
}
By memorizing the list component, we're preventing unnecessary re-renders and improving the performance of our app.
In conclusion, these are just a few examples of the worst code practices in React. By avoiding these mistakes and following best practices, you can create more maintainable, scalable, and efficient code. Remember to always write code that is easy to read, test, and maintain, and you'll be well on your way to becoming a better React developer.