Swastik Yadav

S Y

Navigate back to the homepage

Understanding the need for useEvent() React hook.

Swastik Yadav
January 7th, 2023 · 1 min read

Hello Everyone,

In this post, let’s try to understand the need of useEvent React hook and we will see what problem the React team is trying to solve with this hook.

Note: useEvent is yet not part of React, it is just a proposal to solve the problem of unnecessary rerendering.

2 weeks ago an RFC was proposed by React team in which they introduced an new React hook called the useEvent hook.

Let’s understand that RFC with two examples.

Example One

1import { useState, useCallback } from "react";
2
3export default function App() {
4 const [count, setCount] = useState(0);
5
6 const incrementCount = useCallback(() => {
7 setCount(count + 1);
8 }, [count]);
9
10 return (
11 <div className="App">
12 <span>{count}</span>
13 <button onClick={incrementCount}>SUBSCRIBE</button>
14 </div>
15 );
16}

The above code looks perfectly fine, It is a fairly simple counter component. But the problem is with incrementCount handler.

Whenever the count changes the component rerenders, and on every render the incrementCount handler is created again from scratch.

Well this will not be an issue with such a small example, but in larger applications this may create an issue with performance and optimization.

Even though we have used the useCallback hook, but because useCallback takes count in dependency array the problem remains the same. On every count change useCallback will run again.

Solution: useEvent to the rescue.

useEvent will solve the above problem in two ways:

  • No dependency array: useEvent will have no dependency array, hence no rerendering on every state change.
  • Access to the updated states: useEvent will always have access to the latest states due to closures.

Once useEvent is available to use, the solution will look something similar to the below code.

1import { useState, useEvent } from "react";
2
3export default function App() {
4 const [count, setCount] = useState(0);
5
6 const incrementCount = useEvent(() => {
7 setCount(count + 1);
8 });
9
10 return (
11 <div className="App">
12 <span>{count}</span>
13 <button onClick={incrementCount}>SUBSCRIBE</button>
14 </div>
15 );
16}

Example Two

1import { useState, useEffect } from "react";
2
3export default function App() {
4 const [routeUrl, setRouteUrl] = useState("/home");
5 const [userName, setUserName] = useState("Swastik");
6
7 const logAnalytics = (routeUrl, userName) => {
8 console.log(`Route changed by ${userName} to ${routeUrl}`);
9 };
10
11 useEffect(() => {
12 logAnalytics(routeUrl, userName);
13 }, [logAnalytics, routeUrl, userName]);
14
15 return (
16 <div className="App">
17 </div>
18 );
19}

In the above example analytics is consoled whenever routeUrl or userName changes. But we don’t want that, we only want analytics to be consoled when routeUrl changes not userName.

Why would you log analytics when userName changes?

useCallback again won’t solve that problem because of dependency array. We need something that does not have dependency array and always have access to the updated states.

Well, it seems that a new hook is about to join the list of essential React hooks.

Solution: useEvent to the rescue again.

The solution will look similar to the below code once useEvent is out.

1import { useState, useEffect, useEvent } from "react";
2
3export default function App() {
4 const [routeUrl, setRouteUrl] = useState("/home");
5 const [userName, setUserName] = useState("Swastik");
6
7 const logAnalytics = useEvent((routeUrl) => {
8 console.log(`Route changed by ${userName} to ${routeUrl}`);
9 });
10
11 useEffect(() => {
12 logAnalytics(routeUrl);
13 }, [logAnalytics, routeUrl]);
14
15 return (
16 <div className="App">
17 </div>
18 );
19}

Hopefully you enjoyed this short post. If you are struggling to increase your income as a developer, check out my new course below.

Thank You!

More articles from Swastik Yadav

Packages in React ecosystem that I used in 2022.

The ReactJs ecosystem is constantly growing and evolving, and 2022 was no exception. In this post, we will take a look at some of the npm packages that I used this year.

December 10th, 2022 · 2 min read

The Sass handbook and how does it work?

let's dive deep into the world of CSS preprocessor Sass. We will learn what is Sass, how to use it, and how does it work?

December 10th, 2021 · 3 min read

DiaryOfADev, Success stories of underdog developers.

Every week we share an underdog developer's story and a deep dive into a programming concept.

© 2021–2024 Swastik Yadav
Link to $https://github.com/SwastikyadavLink to $https://www.linkedin.com/in/swastikyadav/