Skip to content

Commit 58d042f

Browse files
committed
Add new error
1 parent d03513f commit 58d042f

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

src/content/reference/react/ViewTransition.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,61 @@ function Component() {
351351
</ViewTransition>
352352
);
353353
}
354-
```
354+
```
355+
356+
### I'm getting an error "There are two `<ViewTransition name=%s>` components with the same name mounted at the same time." {/*two-viewtransition-with-same-name*/}
357+
358+
This error occurs when two `<ViewTransition>` components with the same `name` are mounted at the same time:
359+
360+
361+
```js [3]
362+
function Item() {
363+
// 🚩 All items will get the same "name".
364+
return <ViewTransition name="item">...</ViewTransition>;
365+
}
366+
367+
function ItemList({items}) {
368+
return (
369+
<>
370+
{item.map(item => <Item key={item.id} />)}
371+
</>
372+
);
373+
}
374+
```
375+
376+
This will cause the View Transition to error. In development, React detects this issue to surface it and logs two errors:
377+
378+
<ConsoleBlockMulti>
379+
<ConsoleLogLine level="error">
380+
381+
There are two `<ViewTransition name=%s>` components with the same name mounted at the same time. This is not supported and will cause View Transitions to error. Try to use a more unique name e.g. by using a namespace prefix and adding the id of an item to the name.
382+
{' '}at Item
383+
{' '}at ItemList
384+
385+
</ConsoleLogLine>
386+
387+
<ConsoleLogLine level="error">
388+
389+
The existing `<ViewTransition name=%s>` duplicate has this stack trace.
390+
{' '}at Item
391+
{' '}at ItemList
392+
393+
</ConsoleLogLine>
394+
</ConsoleBlockMulti>
395+
396+
To fix, ensure that there's only one `<ViewTransition>` with the same name mounted at a time in the entire app by ensuring the `name` is uniquie, or adding an `id` to the name:
397+
398+
```js [3]
399+
function Item({id}) {
400+
// ✅ All items will get the same "name".
401+
return <ViewTransition name={`item-${id}`}>...</ViewTransition>;
402+
}
403+
404+
function ItemList({items}) {
405+
return (
406+
<>
407+
{item.map(item => <Item key={item.id} item={item} />)}
408+
</>
409+
);
410+
}
411+
```

0 commit comments

Comments
 (0)