useUpdateMany to update different ids with different data for each id
useUpdateMany({resource:'', ids:IdArray, values: what to give here ?})
id1 has different value than id2 and so on..
id1 has different value than id2 and so on..
useUpdateMany is designed to update multiple records with the same values. If you want to update each record with different values, you should use the useUpdate hook instead. You can loop through the records and call the mutate function for each record with its specific values.useUpdate to update multiple records with different values:Note: you can ask me a follow up question by @ mentioning me again updateMultiple method:useUpdateMultiple hook in your component to update multiple records with different values in a single API call:Note: you can ask me a follow up question by @ mentioning me again useUpdateMany is designed to update the given ids with values. You can make your own implementation using meta.useUpdateManyuseUpdateManyuseUpdateuseUpdatemutateNote: you can ask me a follow up question by @ mentioning me againNote: you can ask me a follow up question by @ mentioning me againupdateMultipleuseUpdateMultipleidsimport { useUpdate } from "@refinedev/core";
const { mutate } = useUpdate();
const updates = [
{ id: "1", values: { title: "New Title 1" } },
{ id: "2", values: { title: "New Title 2" } },
// ...
];
updates.forEach(({ id, values }) => {
mutate({
resource: "posts",
id,
values,
});
});// data-provider.js
export const dataProvider = (apiUrl) => ({
// ...
updateMultiple: async ({ resource, updates }) => {
const url = `${apiUrl}/${resource}/bulk`;
const { data } = await axiosInstance.patch(url, { updates });
return {
data,
};
},
// ...
});// useUpdateMultiple.js
import { useMutation } from "@refinedev/core";
export const useUpdateMultiple = () => {
const { dataProvider } = useContext(RefineContext);
return useMutation(
async ({ resource, updates }) => {
const { data } = await dataProvider.updateMultiple({ resource, updates });
return data;
},
{}
);
};import { useUpdateMultiple } from "./useUpdateMultiple";
const { mutate } = useUpdateMultiple();
const updates = [
{ id: "1", values: { title: "New Title 1" } },
{ id: "2", values: { title: "New Title 2" } },
// ...
];
mutate({
resource: "posts",
updates,
});