#Loader function

1 messages · Page 1 of 1 (latest)

sand ore
#
export const Loader = async ({ params }) => {
    try {
        const data = await customFetch(`/alldevelopers/${params.id}`)
        return data
    } catch (err) {
        toast.error('error')
        return redirect('/dashboard/alldevs')
    }
}

what type should i give to params

and after that

export default function EditDev() {
    const dev = useLoaderData()
    const { name, age, location, skills } = dev.data

it gives me error that dev is of a type unknown

surreal mirage
#

params would have a type like { id: string } most likely based on what you've shared (the whole argument object could be { params: { id: string } }

#

you'll have to share the signature of useLoaderData() for the second part

sand ore
#

if i give { id: string } then my loader complains

    {
                        path: 'edit-dev/:id',
                        element: <EditDev />,
                        loader: editDevLoader
                    },
#

Type '({ params }: { params: { id: string;}; }) => Promise<AxiosResponse<any, any> | Response>' is not assignable to type 'LoaderFunction'.

turbid thunder
#

export const Loader = async ({ params }) => { try { const data = await customFetch(`/alldevelopers/${params.id}`); return data; } catch (err) { toast.error('error'); return redirect('/dashboard/alldevs'); } };

#

export default function EditDev() { const dev = useLoaderData(); const { name, age, location, skills } = dev; // Rest of your code... }

sand ore
#

yes but i work in ts so i need types

turbid thunder
#

({ params }: { params: Params })

sand ore
turbid thunder
#

interface DevData { name: string; age: number; location: string; skills: string[]; }

#

const dev: DevData = useLoaderData(); const { name, age, location, skills } = dev;

sand ore
#

!resolved