Sub topic 7
Passing Reactive Values as Arguments
This chapter assumes you know how to declare an Effect Event with useEffectEvent and that an Effect Event always reads the latest props and state. You should also be comfortable with useEffect dependency arrays and with closures.
An Effect Event can read any value from the component. That raises a design question. When a reactive value matters to both the Effect and the event logic, should the Effect Event read it directly, or should the Effect pass it in as an argument? In most cases, pass it in.
The analytics example
Suppose a Page component logs a visit every time the url changes. Each log entry also records how many items are in the shopping cart. A new url should count as a new visit. A change to the cart should not.
import { useEffect, useEffectEvent } from 'react';
import { logVisit } from './analytics.js';
function Page({ url, shoppingCart }) {
const onVisit = useEffectEvent((visitedUrl) => {
logVisit(visitedUrl, shoppingCart.length);
});
useEffect(() => {
onVisit(url);
}, [url]);
}The Effect reads url, so url stays in the dependency array and the Effect re-runs whenever url changes. The Effect Event reads shoppingCart without making it a dependency, so adding an item to the cart does not log a new visit.
Why not read `url` inside the Effect Event?
You could write onVisit with no parameters and read url directly inside it. The code would still work, but you would lose two things.
The first is intent. When url appears in the Effect body and in the dependency array, a reader can see that a URL change triggers a visit. If the Effect only calls onVisit(), nothing in it refers to url. The linter then sees no reactive values and would accept an empty dependency array, which would log only the first visit. Passing url as an argument keeps the reactive value in the Effect, where the linter tracks it.
The second is the value at the moment of the event. An Effect Event reads the latest props when it runs. If the call happens later, for example after a delay, "latest" may not be the value that caused the Effect to run.
import { useEffect, useEffectEvent } from 'react';
import { logVisit } from './analytics.js';
function Page({ url, shoppingCart }) {
const onVisit = useEffectEvent((visitedUrl) => {
logVisit(visitedUrl, shoppingCart.length);
});
useEffect(() => {
const id = setTimeout(() => {
onVisit(url); // url captured by this Effect run
}, 5000);
return () => clearTimeout(id);
}, [url]);
}The timeout closure captures the url from the render that ran this Effect, and the Effect passes that value in. shoppingCart.length is still read fresh when the timeout fires. Each log entry pairs the URL that triggered it with the cart size at logging time.
A rule of thumb
| Value | Where it goes | Effect |
|---|---|---|
| Should start a new synchronization | Read in the Effect, listed as a dependency, passed as an argument | A change re-runs the Effect |
| Needed only as context | Read inside the Effect Event | A change does not re-run the Effect |
Exercise
A chat component logs logJoin(roomId, username) when it connects. A new roomId should log a new join, but a new username should not. The connection callback runs asynchronously. How should you split the values between the Effect and an Effect Event?
Show answer
Read roomId in the Effect, list it as a dependency, and pass it to an Effect Event such as onJoin(joinedRoomId). Inside onJoin, read username directly and call logJoin(joinedRoomId, username). Because roomId is passed as an argument, the asynchronous callback logs the room that triggered the connection. If it read the latest prop, it could log a different room.
Exercise
A teammate rewrites onVisit to take no arguments, reads url inside it, and calls onVisit() from an Effect with an empty dependency array. The linter reports no warnings. What breaks?
Show answer
The Effect runs only once, on mount, so later URL changes never log a visit. The linter reports nothing because the Effect no longer reads any reactive values. The reactive dependency is now hidden inside the Effect Event, where the linter does not track it.