
All You Need to Know About Teleport in Vue 3
Vue 3's Teleport allows you to move component templates to different parts of the DOM while preserving their Vue context and reactivity, enabling more flexible and powerful UI rendering across your application.
What is Vue Teleport ?
Teleport is a powerful Vue 3 feature that allows you to move a component's template to a different location in the DOM tree while preserving its vue context and reactivity.
It solves common challenges like rendering
When a parent component has
How to use Vue Teleport ?
Here is the simplest way possible to use Teleport in Vue 3.
<template>
<Teleport to="body"> <div> My Element </div>
</Teleport>
</template>
Now if you check your
Actually Vue 3 Teleported our element to
<body>
<div> My Element </div>
</body>
The
prop in Teleport allows you to specify the target element for rendering, accepting either a CSS selector or a direct DOM node reference.to
But the target(
Logical vs Visual Hierarchy
So if you used props, events, and dependency injection, they continue to function as expected.ðŸ¤
<template>
<div class="parent-component">
<Teleport to="body">
<child-component :message="parentMessage" @some-event="handleChildEvent"/>
</Teleport>
</div>
</template>
<script setup>
import ChildComponent from './ChildComponent.vue'
const parentMessage = ref('My prop for child')
const handleChildEvent = (data) => console.log('event from child', data)
</script>
Target Order in Vue Teleport
Based on Vue official doc, several
<body>
<ComponentA/>
<ComponentB/>
</body>
Defer
When
How to Disable the Teleport
The
Instead, it changes how the content is rendered. When
<template>
<Teleport to="body" :disabled="isDisabled">
<!-- Teleport Content -->
</Teleport>
</template>
We can conditionally teleport our component to other location or keep it on its location.
Conclusion
10 Questions and Answers Teleport in Vue 3
Teleport is a built-in component that allows you to render a part of a component's template at a different location in the DOM, while maintaining its original Vue component context and reactivity.
<teleport to="body">
<div>This content will be rendered in the body tag</div>
</teleport>
Yes, Teleported components maintain their full logical hierarchy, meaning they can still receive props, emit events, and use dependency injection just like they would in their original location.
The
5. What's the difference between
- specifies the target location for rendering (can be a CSS selector or DOM node)to
- determines whether the content should be teleported or rendered in its original location, providing a dynamic rendering strategydisabled
6. How is Vue Teleport different from portal-vue used in Vue 2?
Vue Teleport is a built-in component in Vue 3, providing native support for the portal functionality. In contrast, portal-vue was a third-party plugin used in Vue 2 to achieve similar results. Vue Teleport simplifies the implementation and eliminates the need for external dependencies.
7. What happens when multiple Teleports target the same element?
When multiple
8. Can I teleport content inside the Vue app?
While teleporting outside the Vue app is common, you can teleport within the app. However, the target element must already exist in the DOM when the
9. What are React Portal and Vue Teleport, and what problem do they solve?
The key similarity is their core purpose: to break free from the typical component nesting constraints and render components in a different part of the DOM. This is crucial for creating UI elements that need to overlay other content or escape potential styling or positioning restrictions.
// React Portal Example
import React from 'react';
import ReactDOM from 'react-dom';
function ReactModalExample() {
return ReactDOM.createPortal(
<div className="modal">
<h2>React Portal Modal</h2>
<p>This modal is rendered outside the current DOM hierarchy</p>
</div>,
document.body // Renders directly to the body
);
}
10. What are the main differences between
- API Design:
- React Portal uses method, which requires explicitly specifying the target DOM element.ReactDOM.createPortal()
- Vue Teleport uses a built-in component with a declarative<Teleport>prop, offering a more template-friendly approach.to
- React Portal uses
- Implementation:
- React Portal is a method that needs to be imported from 'react-dom'.
- Vue Teleport is a native component in Vue 3, requiring no additional imports beyond the core Vue library.
- Context Preservation:
- Both maintain the original component's context (props, events, state) even when rendering outside the original DOM hierarchy.
- React Portal preserves the React context through the render method.
- Vue Teleport maintains the Vue component's context through its component-based approach.