Does this look right? Loading data for editing form #6076
Unanswered
mercmobily
asked this question in
Q&A
Replies: 2 comments 6 replies
-
have a read here how to avoid this: https://tkdodo.eu/blog/react-query-and-forms |
Beta Was this translation helpful? Give feedback.
6 replies
-
You can include a computed property in the The following code is what I consider to be the best practice: const queryClient = useQueryClient();
const id = computed(() => route.params.id);
let record = ref({});
const { data } = useQuery({
// The `queryKey` allows you to store reactive data, and it will automatically refetch the query
// when the `id` is updated.
queryKey: ['projects', id],
queryFn: () => api.get(id.value),
});
const mutation = useMutation({
mutationFn: data => api.put(id.value, data),
onSuccess: data => {
queryClient.setQueryData(['projects', data.id], data);
},
});
watch(
data,
values => {
if (!values) return;
// update form
record.value = values;
},
{
immediate: true,
}
); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am writing an app generator. I intend to use all of the best practices for everything. However, I have recently moved ro vue/tanstack and I am still working out what the best practices are -- this is especially hard with Vue, since everything has changed in the last year and a half.
Anyhow, this is a file generated by js-kit. I was hoping I could have some feedback in terms of having made huge mistakes.
I have some issues:
Changing tabs, and re-focussing the tab, triggers a reload. This is especially bad since this is an editing screen -- imagine a user typing a long description, then popping onto a new tab to search something, go back to the editing screen, and BANG! everything is... gone!
Cache. I am not sure the cache is being used. When I leave the editing screen (say, I save) and enter it again, it will reload. My understanding of the documentation is that it will provide data, and THEN triggers a reload, and only triggers a mutation if the data has changed. Well 1) I don't see this happening 2) If it DID happen in an editing screen, again, it would be kind of terrible (whereas in a viewing screen it would be OK)
Ideas?
Beta Was this translation helpful? Give feedback.
All reactions