You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/2025/10/01/react-19-2.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ See the [Component track docs](/reference/dev-tools/react-performance-tracks#com
91
91
92
92
### `useEffectEvent` {/*use-effect-event*/}
93
93
94
-
When using Effects you may want to read the most recent props or state inside an Effect without re-running the Effect when those values change. For example, in the following code the `theme` prop is used inside an Effect, but we don’t want the Effect to re-run when `theme` changes:
94
+
One common pattern with useEffect is to notify the app code about some kind of "events" in an external system. For example, a chat room may get connected, and you might want to display a notification when that happens:
95
95
96
96
```js {5,11}
97
97
functionChatRoom({ roomId, theme }) {
@@ -108,29 +108,31 @@ function ChatRoom({ roomId, theme }) {
108
108
// ...
109
109
```
110
110
111
-
To solve this, most users just disable the lint rule and exclude the dependency. But that can lead to bugs since the linter can no longer help you keep the dependencies up to date if you need to update the Effect later.
111
+
The problem with the code above is that any values used inside such an "event" will cause the surrounding Effect to re-run when they change. For example, changing the theme will cause the chat room to reconnect. This behavior makes sense for values that are related to the Effect logic itself, like roomId, but it doesn't make sense for theme.
112
112
113
-
React 19.2 introduces `useEffectEvent`, which lets you declare "Effect Events" that can be called inside Effects. Effect Events always access the latest values from props and state when they are invoked, so you can read the latest values without re-running the Effect:
113
+
To solve this, most users just disable the lint rule and exclude the dependency. But that can lead to bugs since the linter can no longer help you keep the dependencies up to date if you need to update the Effect later.
114
114
115
-
With `useEffectEvent`, we can define the `onConnected` callback as an "Effect Event". Now, it doesn't re-run when the values change, but it can still “see” the latest values of your props and state:
115
+
With `useEffectEvent`, you can split the "event" part of this logic out of the Effect that emits it:
}, [roomId, threadId]); // ✅ All dependencies declared
130
+
}, [roomId]); // ✅ All dependencies declared
131
131
// ...
132
132
```
133
133
134
+
Similar to DOM events, Effect Events always "see" the latest props and state. They should not be declared in the dependency array. They can only be declared in the same component or Hook, which is verified by the linter.
135
+
134
136
<Note>
135
137
136
138
#### When to use `useEffectEvent` {/*when-to-use-useeffectevent*/}
0 commit comments