Though Boussadjra Brahim’s solution works its not the exact answer to the question.
In the sense that reactive data can not be reassigned with =
but there is a way to reassign the reactive data. It is Object.assign
.
Therefore this should work
setup(){
const formData = reactive({})
onMounted(() => {
fetchData().then((data) => {
if (data) {
Object.assign(formData, data) // equivalent to reassign
}
})
})
}
Note:
This solution works when your reactive object is empty or always contains same keys.
However, if for example, formData
has key x
and data
does not have key x
then after Object.assign
, formData
will still have key x
, so this is not strictly reassigning.
demo example; including watch