The problem with useSWR optimistic UI is that await prisma.someThing.create() returns the object that was created.
and we get type error:
Argument of type '{ id: string; imie: string; wiek: number; }' is not assignable to parameter of type '{ id: string; imie: string; wiek: number; }[] | Promise<{ id: string; imie: string; wiek: number; }[] | undefined> | MutatorCallback<{ id: string; imie: string; wiek: number; }[]> | undefined'.ts(2345)
export async function createGirl({ name, age }: { name: string; age: number }) {
await new Promise((res) => setTimeout(res, 1000));
return await prisma.dziewczyna.create({
data: {
imie: name,
wiek: age,
},
});
}
<Button
onClick={() => {
mutate(await createGirl({ age: 12, name: "julia" }), {
optimisticData: [...girls, { wiek: 12, imie: "julia", id: "000" }],
});
}}
>