DEV Community

Cover image for Latest React Tips and Tricks in 2024
Shariq Ahmed
Shariq Ahmed

Posted on • Originally published at Medium

Latest React Tips and Tricks in 2024

React is one of the fastest-growing frameworks for many reasons. Moreover, it’s popular because of its virtual DOM, which makes it really fast. However, for beginners, React can be quite challenging. I also took my sweet time to get good at React. In this article, I will share tips and tricks that are already available on the internet. This article just summarizes those tricks.

Let’s start without further ado.

1. Master the Basics

Get yourself bogged down in fundamentals. Become an expert in it. This is because when the base is strong you can understand even the complex topics easily. Some of the components that you need to master include components, state, props and JSX. And yes, do understand what componentDIdMount — used when a component is mounted to the screen -and componentDiDUpdate — a component that updates the DOM with state changes — are.

2. Learn Next.js After React

This is because whenever you are using a network request in React, then rather than using fetch or Axios, use react-query. Why? It doesn’t only have built-in state managers and a global cache but also a loading state. It can also re-fetch — no matter the component.

Further, if you are using the setState function, then in order to avoid passing extra dependency inside the dependency, use a state value like setState(prev=> //. Again, why? Well, it avoids race conditions.

3. Console.log

In an arrow function, if you want to log out a component or something else, use console.log(/* something */) || after the arrow. Benefits? You will see the results (logged out) without rewriting the function just to make use of the code.

If you want to edit dialogs/modals then instead of going through any laborious process, just add a ‘key’ prop to a dynamic model. It will mention the fresh state of dynamic items.

4. Simple Components

Never ever overcomplicate the component. Keep it simple. Keep the components for just one task. You will understand them easily. Moreover, in order to better the performance and get rid of unnecessary re-renders, use pure components. They only re-render when the state updates. Further, use prototypes — this assists in picking errors in the early stage.

5. Use Hooks

You ain’t gonna regret it. But why? Well, hooks help developers not only in using state but also other React features in functional components. It also prevents overcomplicating the code.

Now, testing React is another area where beginners face difficulties. So here are two testing frameworks that you can use: Jest and Enzyme.

Go for Jest if you are okay with using APIs to write test cases. If you want to test your React’s components function then use Enzyme. Further, it gives a simple API that also helps in rendering and interacting with components.

Top comments (0)