I seem to be missing something with the Vue Composition API when it comes to using and updating reactive objects.
See the below code. I'm expecting the click event to update the {{colors}} output in the template when a color is added.
<template>
<div>
<!-- {{colors}} Is not updated in the DOM on click event -->
<pre>{{ colors }}</pre>
<button @click="addColor">add color</button>
</div>
</template>
<script>
import { reactive } from 'vue';
export default {
setup() {
let colors = reactive({ green: '#ccc000' });
function addColor() {
// By cloning and creating a new merged object
colors = Object.assign({}, colors, { red: '#fff000' });
// Or by changing the object directly
colors.orange = '#322332'; // Also does not work
console.log(colors); // Logs out the correct value
}
return {
colors,
addColor,
};
},
};
</script>
I can see in the console log the value of colors is being updated but not in the DOM.
Here is a code sandbox of the code
https://codesandbox.io/s/mystifying-roentgen-rox9k?file=/src/App.vue