Composition-api: How to assign to reactive object?

Created on 19 Sep 2019  路  3Comments  路  Source: vuejs/composition-api

Is there a way to assign a new value to a reactive object without losing reactivity? If we have an object we expect to be re-assigned, is nesting within a ref the only option?

question

All 3 comments

Making sure I understand the requirement - you arer asking how to do the following:

let reactiveObj = reactive({ /* ... */ })

// ...later:

reactiveObj = reactive({ /* ... a new reactive object ... */})

... but keep reactivity for the reassignment of reactiveObj?

Then yes, the wa to do that is to use a ref:

const reactiveObj = ref({ /* ... */ })

// ...later:

reactiveObj.value = reactive({ /* ... a new reactive object ... */})

Thank you! The idea is that I have some reference object that regularly needs reassigned with a different value. Something like "selectedItem". Wasn't sure if there was a better method.

Anyways, is wrapping the new value in reactive really necessary?

Instead of:
reactiveObj.value = reactive({ /* ... a new reactive object ... */})

Isn't this sufficient:
reactiveObj.value = { /* ... */}

Or is there something I'm missing?

Oh so you only want to reassign a property? That has always worked like you expect it and will work like that here as well of course

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sapphi-red picture sapphi-red  路  4Comments

TooBug picture TooBug  路  6Comments

geekox86 picture geekox86  路  4Comments

banricho picture banricho  路  3Comments

ycmjason picture ycmjason  路  5Comments