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
{{ message }}
Repository navigation
Dependency Injection inheritance fails when provided after grandchildren are rendered. #13921
🔨 p3-minor-bugPriority 3: this fixes a bug, but is an edge case that only affects very specific usage.has workaroundA workaround has been found to avoid the problemneed discussion
Create 4 components, each a child of the other.
App -> 1 -> 2 -> 3 -> 4
App and Component 1 each provides a unique value for the key "test" on first render. When Component 2 is re-rendered provide a unique value for the key "test".
What is expected?
It should be expected that Component 4 inherits the new provided value of Component 2, but instead only Component 3 inherits.
What is actually happening?
When provide() is called, it creates a new Object with the prototype of the previous parent's providers, however when this is done in overlap like it is here, it skips one.
First Render:
Component 1 has Object.create(App.provides)
Component 2 has Component 1's provides
Component 3 has Component 2's provides (which is Component 1's provides)
Component 4 has Component 3's provides (which is Component 1's provides)
Second Render:
Component 1 has Object.create(App.provides)
Component 2 has Object.create(Component 1.provides)
Component 3 has Component 2's provides
Component 4 has Component 3's old provides (which is Component 1's provides) [Note: Here lies the issue.]
There are two solutions as I see:
A) Object.create, capture parent prototype immediately when Component Instance is created.
B) Update prototype on every rerender.
I don't think calling provide inside a lifecycle hook is supported. It may work in some cases, but I think that's by chance rather than by design. The provided values are expected to be locked in by the time the component renders.
This is touched upon in the docs, though maybe not as explicitly as it could be. e.g.:
If not using <script setup>, make sure provide() is called synchronously inside setup():
It doesn't explicitly explain how it works with <script setup>. I believe in that case it either needs to be called synchronously or after a top-level await with Suspense.
I wonder whether we could/should add better warnings to provide to avoid this kind of usage?
It should be supported now by my PR, but my PR does not resolve the issue of provide() inside of render (which would need to be resolved by using currentRenderingInstance). Generally you wouldn't need to do anything except return VNode tree in render
Right because in async, the currentInstance would be unset by time it's executed, because Microtasks (which async is ran) can only be run when current Execution Task is finished, and hence the currentInstance is reset.
The solution would be to have a function of component public instance where you can provide and inject.
🔨 p3-minor-bugPriority 3: this fixes a bug, but is an edge case that only affects very specific usage.has workaroundA workaround has been found to avoid the problemneed discussion
Activity
edison1105 commentedon Sep 24, 2025
a workaround:
benjamin-larsen commentedon Sep 24, 2025
There are two solutions as I see:
A) Object.create, capture parent prototype immediately when Component Instance is created.
B) Update prototype on every rerender.
I can make pull requests for both of these.
skirtles-code commentedon Sep 25, 2025
I don't think calling
provideinside a lifecycle hook is supported. It may work in some cases, but I think that's by chance rather than by design. The provided values are expected to be locked in by the time the component renders.This is touched upon in the docs, though maybe not as explicitly as it could be. e.g.:
https://vuejs.org/api/composition-api-dependency-injection.html#provide
https://vuejs.org/guide/components/provide-inject.html#provide (with API toggle set to Composition)
It doesn't explicitly explain how it works with
<script setup>. I believe in that case it either needs to be called synchronously or after a top-levelawaitwith Suspense.I wonder whether we could/should add better warnings to
provideto avoid this kind of usage?benjamin-larsen commentedon Sep 25, 2025
It should be supported now by my PR, but my PR does not resolve the issue of provide() inside of render (which would need to be resolved by using currentRenderingInstance). Generally you wouldn't need to do anything except return VNode tree in render
edison1105 commentedon Sep 25, 2025
The current reproduction works because
onBeforeMountandonBeforeUpdateare executed synchronously.benjamin-larsen commentedon Sep 25, 2025
Right because in async, the currentInstance would be unset by time it's executed, because Microtasks (which async is ran) can only be run when current Execution Task is finished, and hence the currentInstance is reset.
The solution would be to have a function of component public instance where you can provide and inject.
benjamin-larsen commentedon Sep 27, 2025
@edison1105 I could probably make a component instance method (like $forceUpdate) for $provide and $inject that would work async.
fix(provide): warn when using `provide` after mounting (#13954)