Skip to content

Commit 2c57cc7

Browse files
committed
docs: add useEffectEvent section in built-in hooks
1 parent f8c81a0 commit 2c57cc7

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/content/reference/react/hooks.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ function ChatRoom({ roomId }) {
7474
7575
Effects are an "escape hatch" from the React paradigm. Don't use Effects to orchestrate the data flow of your application. If you're not interacting with an external system, [you might not need an Effect.](/learn/you-might-not-need-an-effect)
7676
77+
* [`useEffectEvent`](/reference/react/useEffectEvent) extracts non-reactive logic from your Effects into a reusable function.
78+
79+
```js
80+
function ChatRoom({ roomId, theme }) {
81+
const onConnected = useEffectEvent(() => {
82+
showNotification('Connected!', theme);
83+
});
84+
85+
useEffect(() => {
86+
const connection = createConnection(serverUrl, roomId);
87+
connection.on('connected', () => {
88+
onConnected();
89+
});
90+
connection.connect();
91+
return () => connection.disconnect();
92+
}, [roomId]);
93+
94+
// ...
95+
}
96+
```
97+
98+
This can help avoid issues with stale values inside Effects and lets you keep your effect logic organized and up to date.
99+
77100
There are two rarely used variations of `useEffect` with differences in timing:
78101
79102
* [`useLayoutEffect`](/reference/react/useLayoutEffect) fires before the browser repaints the screen. You can measure layout here.
@@ -87,8 +110,8 @@ A common way to optimize re-rendering performance is to skip unnecessary work. F
87110
88111
To skip calculations and unnecessary re-rendering, use one of these Hooks:
89112
90-
- [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
91-
- [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
113+
* [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
114+
* [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
92115
93116
```js
94117
function TodoList({ todos, tab, theme }) {
@@ -101,18 +124,19 @@ Sometimes, you can't skip re-rendering because the screen actually needs to upda
101124
102125
To prioritize rendering, use one of these Hooks:
103126
104-
- [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
105-
- [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
127+
* [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
128+
* [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
106129
107130
---
108131
109132
## Other Hooks {/*other-hooks*/}
110133
111134
These Hooks are mostly useful to library authors and aren't commonly used in the application code.
112135
113-
- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
114-
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
115-
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
136+
* [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
137+
* [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
138+
* [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
139+
116140
* [`useActionState`](/reference/react/useActionState) allows you to manage state of actions.
117141
118142
---

0 commit comments

Comments
 (0)