SolidJSS
SolidJSโ€ข3y agoโ€ข
19 replies
Miabread

useRouteData returning undefined

hi I'm trying to get the example from https://start.solidjs.com/core-concepts/data-loading to work but instead I always get this error
I've tried wrapping it in Show or doing if (!students) return; but that causes hot module reloading to not work at all because I think it's never actually loading properly client side
import { For, Accessor, createResource } from 'solid-js';
import { useRouteData, createRouteData } from 'solid-start';

type Student = { name: string; house: string };

export function routeData() {
    return createRouteData(async () => {
        const response = await fetch('https://hogwarts.deno.dev/students');
        return (await response.json()) as Student[];
    });
}

export default function Page() {
    const students = useRouteData<typeof routeData>();

    return (
        <ul>
            <For each={students()}>{(student) => <li>{student.name}</li>}</For>
        </ul>
    );
}
image.png
Was this page helpful?