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: 75 Replies
If I do something like
console.log(data())
it would be undefined, every time I try to use it.Will
url
have an initial value when the resource is called?
I see you're calling createUrl(p)
but does it return a url when the useFetch
is used?
My guess is that the resource isn't rerunwhat?
If you want the resource to rerun based on the url signal then you need to pass it in as a source as the first parameter of createResource
I'm not wanting to rerun it yet
Just wanting for it to work.
Okay, try replacing
url()
with a static url to fetch from...
I'm trying to see where the issue is
What?
.
I didn't understand anything from that
Okay, here try this.
Does this return not undefined
That'll return undefined
since it's the same code
You tried it?
It's the same code, it should have the same output.
If your question is if the
url
is valid, yes it is correct.Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
It doesn't have the same output,
it should have, though.
Why?
.
and the url is valid.
The issue is the url that the resource is trying to access. Meaning,
url
doesn't have a valid value. If you console.log url()
inside the resource before the return what do you get?A valid url.
Like I said, the url is valid.
the url itself that you want to fetch from might be valid. I'm saying that the
url
signal doesn't have a valid value yet
Yes, I understandyes, it does.
It literally fetchs.
If I do this This would print a valid response
But the return of
createResource
would still be undefinedokay, so where are you using the data value?
In a component
Could you show how you're using it?
I can't seem to replicate it, so the issue could be something else
Or make a reproduction in the playground
const {data} = useFetch({url})
}}Where is the
data
function being used?Im passing to other components
why is that relevant?
Because I can't replicate the issue, so it could be something else. If you think the issue is still
createResource
, can you make a minimal reproduction of the issue in the playgrounddone
Okay, putting this in the playground I don't get undefined
https://playground.solidjs.com/anonymous/8e08f3af-dcea-482d-a1cd-1de21d46fbec
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
So, the issue could be something else..
solid problem them
bc it doesnt work locally
It could be how you're using the data value
Could you show how you're using it? You say you're passing it to components, but how are the components using it?
Im just doing prop spreading.
{ ...data() }
okay, what if you do
{...data()}
I just did a typo there, not used to solid
Ah okay. Hmm and console logging the data in a createEffect like I did in the playground example still prints out undefined?
in the parent component
that uses the
useFetch
yes, even if I do it outside or ir inside a createEfct
outside a createEffect it will be undefined
the data is just always undedined
Okay, the best way for me to help you find out the issue is if you can make a reproduction in the playground
I'm not getting any info to be able to tell what the issue is
sec, my laptop's keyboard is a bit broken
https://playground.solidjs.com/anonymous/393cfa9f-7c19-4ff9-baf6-05fb82fbf4d9 same thing as local
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Kinda same, at least
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
replace use of
didFetch
with joke()
https://playground.solidjs.com/anonymous/3771abfd-bbc5-43c3-be5e-a518ca191412
Or make didFetch a function
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
?
that shouldn't change anything?
didFetch isn't reactive
what about it?
so it will always be false
But if I do it without the show, it's still undefined.
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Locally it goes to the fallback forever
Which fallback? I removed Show in that example
It just shows nothing*
It's undefined.
And why does a random fn changes everything?
Because variables aren't reactive
Here
nextValue
won't update when the signal
changes, but nextValueFn
willBut both are calling the signal
both of them should be changed
Yes, but in Solid only functions are tracked and updated
It's the reason why using console.log outside a createEffect won't update
This will print 1 and then never print when the signal updates
and wait, that doesn't work.
If you want to use didFetch without using a function explicitly, you can do it this way
https://playground.solidjs.com/anonymous/5ec53eb0-36be-4358-9af0-d7487f6adcad
But you can't destructure the return of useFetch
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
https://prnt.sc/nInWrBQEVKhl Locally.
hmmm
didFetch is a function?
yes, I'm even calling it.
Hmmm, and the fallback isn't showing?
no, just a blank page
which just means it's undefined.
Any errors in the console? Because if nothing is showing up then it's something else
At least
Joke:
should be showingNo, just vite stuff.
The browser console?
Both.
If
<h1>Joke:</h1>
from here is not showing up, it must be something else
Was this ever resolved?