When it comes to React, there are some of the things that will optimize your code and will give you better results in it. We will be seeing the same set of habits you can apply. Here, I will share the five habits that I use in developing a React App.

#1 Never Optimize it from the Start

There is a huge mistake that I used to make. I used to optimize the code from the start which was a very bad habit.

The simple reason for this is because you don’t need to optimize the code from the start unless there are some issues.

In most cases, the React app might cause performance issues due to the reconciliation process and nothing else.

#2 Using Error Boundaries

Hate it when you see a lot of errors on your app? Well, it’s common to have errors but it shouldn’t affect your entire program. That’s where you can use error boundaries to wrap up the components. It’s the most useful feature.

The concept is to wrap the component and catch the error using the componentDidCatch method. So, a single error won’t crash the app.

#3 Lazy Loading

My third habit is using lazy loading in the program to optimize performance. Like I mentioned in the first habit, I don’t optimize the entire program from start but I surely add a small line for lazy loading.

It’s a feature released in v16.6 and offers a better direct way to load the components to react. A single line is enough to load all the components.


#4 Using Extensions

You can surely use React Extensions to help you with many things. One of the most popular extensions that I use is to React DevTools. This extension will help you at many possible places you can imagine. It helps you understand the code easily.

One of the features you will get here is when you visit any website that uses React, you can see the details in the console. This will allow you to understand their code and you can implement this in your code too.

Many similar extensions might help you depending on the apps you are working with.

#5 Test the Code Efficiently

It’s always a hard thing to test the code if you don’t use proper techniques. That’s why I use Jest + Enzymes to test the code more efficiently. Now, you no longer need to use the tested loop to see if the code runs correctly or not. You can simply test it with Jest and Enzymes. We can even use this to test the props.

Final Words

To conclude, these are five habits that I use when I write React code. Along with this, some of the common habits such as commenting and using smart code wherever possible are always there. Here, I have shared some of the lesser-known habits to ensure that you get proper value out of it.

You can also use TypeScript along with React to get better results.


This article is republished from hackernoon.com

Previous Intel Is Foundational To Unleashing The Possibilities Of 5G
Next Ensuring Privacy With Zero-Party Data