Sébastien TIMONER
Expert in web development and team management, I specialize in creating and optimizing high-performance digital solutions. With extensive expertise in modern technologies like React.js, Node.js, TypeScript, Symfony, and Zephyr OS for IoT, I ensure the success of complex SaaS and IoT projects, from design to production, for companies across various sectors, at offroadLabs.
At offroadLabs, I offer custom development services that combine technical expertise with a collaborative approach. Whether creating an innovative SaaS solution, developing IoT systems with Zephyr OS, modernizing an existing application, or supporting the upskilling of a team, I am committed to delivering robust and high-performance solutions tailored to the specific needs of each project.
I am available for projects in the Aix-en-Provence area or fully remote.
In React projects, we often use useEffect
reflexively, but this approach can sometimes unnecessarily complicate your code. In this article, we'll look at cases where you don't need useEffect
and how to optimize your code by avoiding its misuse.
useEffect
The useEffect
hook is useful for managing side effects like subscriptions or operations that need to happen outside the main rendering flow. However, some common use cases can be simplified without useEffect
.
useEffect
You often want to initialize state based on props or initial values when mounting a component. Instead of using useEffect
to set this state, you can do it directly with useState
, which will make your code clearer and prevent unnecessary calls.
Bad practice with useEffect
:
typescript
Good practice without useEffect
:
Here, we can simply initialize the greeting
state using a function in useState
:
typescript
This approach makes the code more concise and avoids adding a superfluous effect that could slow down your application.
If you need to calculate a value based on state or props, try using a derived variable or memo
rather than useEffect
.
Example with useEffect
(to avoid):
typescript
Solution without useEffect
:
Here, the total
value can be calculated directly during rendering, without depending on useEffect
:
typescript
This method is not only cleaner but eliminates the need to manage additional state.
useMemo
for Heavy CalculationsFor expensive calculations or those dependent on multiple variables, use useMemo
to avoid re-executing the logic on each render, without needing useEffect
to manage state.
Example with useEffect
:
typescript
Optimized approach with useMemo
:
typescript
Here, useMemo
allows memorizing the result without having to manage an effect, making the component more predictable and performant.
For managing controlled inputs, you don't need to use useEffect
to synchronize input values with parent component state. Simply use props and callback functions to manage this state efficiently.
Example with useEffect
(unnecessary):
typescript
Solution without useEffect
:
typescript
Directly using props simplifies the code and improves performance by reducing unnecessary state updates.
If you want to apply styles or classes based on state, it's often unnecessary to use useEffect
. React handles this type of synchronization well during rendering.
Example with useEffect
:
typescript
Solution without useEffect
:
typescript
Avoiding useEffect
often allows you to simplify your code and improve your React application's performance. By reevaluating the reasons for using an effect, you can create clearer components and avoid errors related to poorly configured dependencies. The key is identifying cases where state, derived values, or useMemo
are sufficient to meet your needs without side effects.