-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject/Provide is not reactive #7017
Comments
Unfortunately, that's by design. you can hack around it using an object an referring to its children, but it's not recommended because it could break in future versions of Vue |
I'd like to point out two possible solutions to this problem:
// parent:
{
provide() {
return {
$computedProperty: () => this.computedProperty
}
},
computed: {
computedProperty() {
// Returns the result of some potentially reactive computation
}
}
}
// children:
{
inject: [
'$computedProperty'
],
computed: {
computedProperty() {
return this.$computedProperty()
}
}
} |
That second trick is great, never thought of that! |
@LinusBorg a function used as a tunnel / wormhole through the non-reactive JS space-continuum :) |
@lehni provide() {
return {
$computedProperty: () => this.computedProperty,
};
}, |
@Tofandel hmmm but you have the same code there as I do? And I'm using this in production, sure works for me... |
@lehni Haha okay I thought you edited it so I was confused :p |
This must be added to the recipes or guides. |
I have discovered new way: // parent:
{
provide() {
const _this = this;
return {
get computedProperty () {
return _this.computedProperty;
}
}
},
computed: {
computedProperty() {
// Returns the result of some potentially reactive computation
}
}
}
// children:
{
inject: [
'computedProperty'
],
} You don't need computed method in children anymore. |
I discovered a flaw in @lehni 's trick today. Wrapping the object in a function triggers an error when the providing object gets unmounted I think, as if the injection briefly couldn't be resolved anymore:
This error doesn't happen when a plain object is passed. // children:
{
inject: [
'$computedProperty': {
// you need to provide a default object that doesn't trigger errors
// in your component when used
default: () => () => {}
}
],
computed: {
computedProperty() {
return this.$computedProperty()
}
}
} It feels dirty overall. We should probably avoid passing functions. @cihad I haven't been able to make your trick work, it's not reactive for me. And I needed two different names to avoid a loop: |
It seems like the only way to get it to work is by Has there been any progress on opinions of how to tackle this? I dont want to prop drill or use state management. Provide/Inject seems like the best solution, but feels weird we need to hack around the cleanest solution. Thoughts? |
In case anyone is interested, I've built a plugin that implements a simple API for reactive provide/inject basing on @lehni's trick: https://github.com/adamsol/vue-component-store. |
You can inject reactive variables with the composition API
|
Version
2.5.3
Reproduction link
https://codesandbox.io/s/7o2qp4l3z6
Steps to reproduce
Parent Component
Child Component
What is expected?
The challenge property should change when its parent this.challenge changes on the proxy
What is actually happening?
Not changing at all
The link provided is just an example how to reimplement what i'm really doing but that is the same code that i use
any ideas?
The text was updated successfully, but these errors were encountered: