-
Notifications
You must be signed in to change notification settings - Fork 345
attrs is not reactive when destructured #264
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
Comments
Ok, solved this by not destructuring the context parameter like this: <template>
<div>
{{ $attrs['hello-world'] }}
{{ myComputed }}
</div>
</template>
<script>
import { defineComponent, watch, computed } from "@vue/composition-api";
export default defineComponent({
inheritAttrs: false,
setup(props, context) {
const myComputed = computed(() => {
return context.attrs['hello-world']
});
return {
myComputed
}
}
});
</script> Lesson learned, but I wish the docs didn't send me into the wrong direction, this is really not correct:
const MyComponent = {
setup(props, { attrs }) {
// a function that may get called at a later stage
function onClick() {
console.log(attrs.foo) // guaranteed to be the latest reference
}
}
} |
Seems to be a bug, rather than a docs issue. But I still need to verify it though as you didn't provide runnable code. The docs definitely decribe the intended behaviour, and Line 223 in 8cea63a
[...] Lines 238 to 243 in 8cea63a
edit: at second look, this implementation is indeed buggy in that regard as the destructuring does break the reference to Not too sure this is solvable without involving an Es6 proxy. |
I'm having the exact same issue when I'm destructuring props: // Composable that changes vuex state
async function deleteProviderFile(id) {
try {
await $api.delete(`providers/${currentProviderId.value}/files/${id}`)
// I see the mutation happen in Vuex devtools
return getProviderField('files').value = getProviderField('files')
.value
.filter(f => f.id !== id)
} catch (e) {
// TODO error alert
}
}
// component destructuring props: removing a picture doesn't update the rendered pictures
setup({ pictures }) {
const state = reactive({
featuredPicture: computed(() => pictures[0]),
firstPictures: computed(() => pictures.slice(1, 5)),
otherPictures: computed(() => pictures.slice(5)),
})
return {
state,
}
},
// without destructuring: the UI updates accordingly
setup(props) {
const state = reactive({
featuredPicture: computed(() => props.pictures[0]),
firstPictures: computed(() => props.pictures.slice(1, 5)),
otherPictures: computed(() => props.pictures.slice(5)),
})
return {
state,
}
}, I'm sorry if there's a more relevant issue, it's actually this one that led me to try without destructuring and fixed my months old bug (which I thought was a brain bug of mine). |
Destructuring props is indeed not supported in that way. You would have to call That's explained in the RFC I think |
I was somehow expecting such an answer where I would have badly read either the docs here or the RFC. Very sorry 😅 and thanks a lot @LinusBorg for replying despite my lack of attention! EDIT: here's the related part of the RFC https://vue-composition-api-rfc.netlify.app/#ref-vs-reactive |
@antfu Can I ask why this was closed? This is still an issue as far as I know. |
This is expected behavior as pointed out in the last comments ⬆️ |
The last comments were in relation to |
Sorry, I missed reading the issue so I closed it. However, just like Linus pointed out in this comment #264 (comment), there is no clear way to solve this as destructuring context will break the proxy to |
Yeah that might be a good idea if this can't be solved. Not to be confrontational, but are you fine with this package not mirroring the functionality of the Vue 3 composition API? EDIT: I see now that there are some limitations in the README. |
Please don't close this issue because VCA official doc mentioned that |
@antfu why doesn't it have the same functionality on Vue 3? It's still not reactive. |
Minimal (non-)working example:
The value in the template,
$attrs
changes accordingly, but the computed value doesn't.The text was updated successfully, but these errors were encountered: