I want to update my reactive form object after data was taken from Api. When I refreshed page, I can see it was changed on console.log() but it does not updated on DOM.
When Dom was mounted, i fill my form object but it is reactive object so it needs to be triggered with a right way . Normally i solved this problem by using watch but i dont know how to solve this problem without using watch and only with mounted
userInformation.vue
<template>
b {{form}} //does not triggered
</template>
<script>
import { getUserInformation } from '@/utils/userInformation';
export default {
setup(){
const { store, $axios, i18n } = useContext();
const { userForm } = getUserInformation();
const form = reactive({
fullName: '',
userName: '',
aboutMe: '',
photoUrl: '',
});
onMounted(() => {
if (userForm)
{
console.log('userForm',userForm) //data comes and for updated
fillForm(userInfo.value); }
});
//if I use watch in here, form object will updated on DOM, but
//i think no need to use watch because userForm gets necessary values in onMounted
const fillForm = async data => {
if (data) {
form.fullName = data.fullName || '';
form.userName = data.nick || '';
form.aboutMe = data.aboutMe || '';
form.photoUrl = data.photoURL || '';
}
};
return {form}
}
}
userInformation.js file
In this file, i get all informations about user and it returns userForm. I prefered to use composable function . Here works well and it returns necessary informations about user
import { computed, onMounted, reactive, useContext, watch } from '@nuxtjs/composition-api';
export function getUserInformation() {
const { store } = useContext();
const userInfo = computed(() => store.state['profile'].profileData);
const userForm = reactive({
fullName: '',
userName: '',
aboutMe: '',
photoUrl: '',
});
const fillForm = async data => {
if (data) {
userForm.fullName = data.fullName || '';
userForm.userName = data.nick || '';
userForm.aboutMe = data.aboutMe || '';
userForm.photoUrl = data.photoURL || '';
}
};
onMounted(async () => {
if (userInfo.value) await fillForm(userInfo.value);
});
watch(userInfo, async info => {
if (info) await fillForm(info);
});
return { userForm };
}