feat(teleport): support deferred Teleport #11387
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
close #2015
close #11386
This pull request introduces a new prop for the built-in
Teleport
component:defer
.A deferred Teleport will wait until all other DOM content in the same update cycle has been rendered before locating the target container and mounting its children, so this is now possible:
Even if
<div#target>
appears after<Teleport>
, it can still be resolved. This also works even if<div#target>
is rendered by other components, as long as they are mounted in the same tick with the component containing the<Teleport>
.Usage with Suspense
Another use case for
defer
is inside<Suspense>
. Previously,<Teleport>
inside<Suspense>
will eagerly resolve its target and therefore can only target containers outside of the<Suspense>
. Withdefer
, the target resolution happens when the Suspense is resolved, after resolved content is inserted into the DOM:Implications of Defer Mode
This behavior is only enabled via the
defer
prop because it can lead to different lifecycle call order compared to the default behavior. For example,mounted
hooks of components inside a deferred Teleport tree will be called after themounted
hooks of the parent. The behavior of deferred vs. non-deferred Teleports are also completely different when placed inside<Suspense>
.