SolidJSS
SolidJS3y ago
104 replies
Sun

createResource returning undefined when data is fetched

I'm just learning solid and did something like a hook? maybe? dunno.

Anyways, I'm just calling createResource with a fetch on it and returning it's json.

But, for any reason, that fn is returning undefined, even when the data is properly fetched("tested" by logging the json).

Code:
const useFetch = <T>(p: UseFetchParams) => {
  const [url] = createSignal(createUrl(p));

  const [data] = createResource<T>(async () => {
    return await fetch(url()).then(async (res) => (await res.json()) as T);
  });

  return { data, didFetch: data() !== undefined };
};
Was this page helpful?