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
In this example, `itemsRef` doesn't hold a single DOM node. Instead, it holds a [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map) from item ID to a DOM node. ([Refs can hold any values!](/learn/referencing-values-with-refs)) The [`ref` callback](/reference/react-dom/components/common#ref-callback) on every list item takes care to update the Map:
@@ -327,17 +330,39 @@ In this example, `itemsRef` doesn't hold a single DOM node. Instead, it holds a
327
330
constmap=getMap();
328
331
if (node) {
329
332
// Add to the Map
330
-
map.set(cat.id, node);
333
+
map.set(cat, node);
331
334
} else {
332
335
// Remove from the Map
333
-
map.delete(cat.id);
336
+
map.delete(cat);
334
337
}
335
338
}}
336
339
>
337
340
```
338
341
339
342
This lets you read individual DOM nodes from the Map later.
340
343
344
+
<Canary>
345
+
346
+
This example shows another approach for managing the Map with a `ref` callback cleanup function.
347
+
348
+
```js
349
+
<li
350
+
key={cat.id}
351
+
ref={node=> {
352
+
constmap=getMap();
353
+
// Add to the Map
354
+
map.set(cat, node);
355
+
356
+
return () => {
357
+
// Remove from the Map
358
+
map.delete(cat);
359
+
};
360
+
}}
361
+
>
362
+
```
363
+
364
+
</Canary>
365
+
341
366
</DeepDive>
342
367
343
368
## Accessing another component's DOM nodes {/*accessing-another-components-dom-nodes*/}
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/components/common.md
+23-2
Original file line number
Diff line number
Diff line change
@@ -257,11 +257,32 @@ React will also call your `ref` callback whenever you pass a *different* `ref` c
257
257
258
258
#### Parameters {/*ref-callback-parameters*/}
259
259
260
-
*`node`: A DOM node or `null`. React will pass you the DOM node when the ref gets attached, and `null` when the ref gets detached. Unless you pass the same function reference for the `ref` callback on every render, the callback will get temporarily detached and re-attached during every re-render of the component.
260
+
*`node`: A DOM node or `null`. React will pass you the DOM node when the ref gets attached, and `null` when the `ref` gets detached. Unless you pass the same function reference for the `ref` callback on every render, the callback will get temporarily detached and re-attached during every re-render of the component.
261
+
262
+
<Canary>
261
263
262
264
#### Returns {/*returns*/}
263
265
264
-
Do not return anything from the `ref` callback.
266
+
***optional**`cleanup function`: When the `ref` is detached, React will call the cleanup function. If a function is not returned by the `ref` callback, React will call the callback again with `null` as the argument when the `ref` gets detached.
267
+
268
+
```js
269
+
270
+
<div ref={(node) => {
271
+
console.log(node);
272
+
273
+
return () => {
274
+
console.log('Clean up', node)
275
+
}
276
+
}}>
277
+
278
+
```
279
+
280
+
#### Caveats {/*caveats*/}
281
+
282
+
* When Strict Mode is on, React will **run one extra development-only setup+cleanup cycle** before the first real setup. This is a stress-test that ensures that your cleanup logic "mirrors" your setup logic and that it stops or undoes whatever the setup is doing. If this causes a problem, implement the cleanup function.
283
+
* When you pass a *different*`ref` callback, React will call the *previous* callback's cleanup function if provided. If not cleanup function is defined, the `ref` callback will be called with `null` as the argument. The *next* function will be called with the DOM node.
0 commit comments