React JS Best Practices

Michael Mitrakos
Published in
4 min readDec 26, 2022

--

Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!

React is a popular JavaScript library for building user interfaces, and it has a strong ecosystem of tools, libraries, and best practices that can help developers build efficient and maintainable applications. Here are some best practices to keep in mind when working with React:

Use functional components whenever possible

Functional components in React are the no-nonsense, streamlined alternative to class-based components. Think of them like sports cars: sleek, easy to understand, and efficient. They often do the same jobs as their class-based counterparts but without the extra bells and whistles. What makes them even better is that they’re more performance-friendly. They don’t carry the weight of state management and lifecycle methods, so your code runs smoother and faster. It’s a win-win for both you and anyone who has to read or work with your code later on.

Use hooks

Hooks in React are a game changer; they’ve only been around for a bit but they let you tap into state and other goodies without the hassle of writing a class. Your code ends up cleaner and easier to get, plus you might even see a speed boost since you’re dodging the extra baggage that comes with class components. Honestly, unless you’re messing around with some old-school React project, you’re probably not going to bump into class components much these days.

Use the React context API for shared state

The context API is like the VIP backstage pass for your React components. It allows them to share what’s going on (aka “state”) without having to relay the message down a whole chain of middlemen. Imagine not having to pass props through multiple levels of your component tree — sounds dreamy, right? It not only makes your code easier to read and understand, but it also gives your app’s performance a leg up by dodging that annoying bottleneck known as prop-drilling.

Use the React memo HOC for performance

The React memo is essentially your go-to for supercharging the efficiency of your functional components. Think of it as your component’s memory bank; it keeps tabs on how your component rendered last time and reuses that information. The catch is, it only bothers to update and re-render the component if there are changes in the props. So if nothing’s new, no extra work is done. This is a big win because it can dramatically cut down on the computational grunt work, meaning your app not only runs smoother but also eats up less of your device’s resources. All in all, it’s a nifty tool that can make a tangible difference in your app’s overall performance and user experience.

Note: With all things, there is a little more complexity than meets the eye. I suggest you read more about memoization (useMemo) in React to learn when exactly you should and shouldn’t use it. Here’s a good resource.

Use the React dev tools

The React dev tools are like x-ray glasses for your code, but in browser extension form. You can dig into your app’s component tree, see what’s going on with the state and props of each piece, and even do some hands-on debugging. It’s basically a must-have for getting under the hood of your app and nailing down any issues.

Follow the React style guide

The React style guide is like your coding bible for writing clean, fuss-free code. It’s got tips on everything — from what to name your files to how to lay out your code so it’s easy on the eyes. Follow it, and not only will your projects be easier to handle, but your teammates will also have an easier time getting what you’re doing.

Use a linter

A linter is basically a software buddy that scans your code to flag errors and potential problems. Trust me, it’s a lifesaver for catching hiccups before they turn into major headaches. If you’re coding in React, you’re in luck — there are tons of linters to choose from. I’ve been using ESlint for nearly a decade, and it’s been a game-changer for keeping my code clean and dependable.

I founded Higglo Digital, where we specialize in giving businesses a killer online presence. We craft top-notch websites and digital strategies that could win awards. If you want to see a beautifully designed website, check us out.

If you’ve got a case of wanderlust, you’ll want to check out another project of mine: the Wanderlust Extension. It’s your virtual passport to the world’s most stunning locations, all handpicked for your awe and inspiration. Give it a whirl!

--

--