- You have to define a loader in the index.tsx file, outside of the component and export it:
export const useAddProduct = routeLoader$(async () => {
return await prisma.products.create({
data: {
name: 'Steel Table',
description: 'A steel table added to DB'
}
})
- You call this loader from inside your component:
const handleSubmit = $(() => {useAddProduct()})
My problem is how to tell the loader about the data object, since no parameters are accepted? When fetching by id I could use params:
export const useProductById = routeLoader$(async ({ params }) => {
if (params.id) {
const id = parseInt(params.id)
return await prisma.products.findUnique({
where: { id }
})
}
return []
});
Are there similar contexts apart from { url, method, params } I should know of?
Every hint in the right direction is greatly appreciated!