#Qwik signals and useOnWindow
57 messages Β· Page 1 of 1 (latest)
Here's the example code that was linked in the general channel: https://sourceb.in/mrMPxmhlG1
yes
I have a suspicion of what's wrong, but let me confirm before I lead you down a wrong track π
alright
oh btw the reason im putting it inside a useOnWindow callback is that when i dont do that my code only runs once and the signal never re-renders the page again
Sorry, I don't think it's the issue I was suspecting π¦
Do you think you could make a more minimal reproduction of the issue? I'm able to use signals in a window load event here: https://stackblitz.com/edit/qwik-starter-pmz3xq?file=src%2Froutes%2Findex.tsx
sure
so you couldent reproduce it?
That's right. I also didn't get the socketio stuff working though
yeah you cant just copy my code and make it work
cuz there is my server code running on my machine on localhost
it doesnt matter tho i can still reproduce
Right, which is why a minimal reproduction that doesn't require your server would be nice π
Some little notes that I don't think should cause errors, but might make things a little better.
- instead of useOnWindow, you can use
useBrowserVisibleTask$to run code in the browser when it's first loaded. By default, it will run the code when the component is visible, You can usestrategy: 'document-ready'to run on the document load event regardless of if the component is visible - To track an object, I would recomend using useStore instead of useSignal. Signals are better for basic types like strings, booleans, numbers, etc but won't track changes within the object.
In my linked stackblitz, I do things in both your way and my way (but your way required some extra changes to get the state change to be reflected)
Since my stackblitz can access a signal in the useWindow callback, I think there's another piece causing the problem here.
perhaps the version?
"@builder.io/qwik": "^0.19.2",
"@builder.io/qwik-city": "^0.4.0",
im a bit behind
I just changed to those versions and it's still working
I need to head out but if your wondering this is the error i get
i dont know why but stuff like this just happens sometimes
this framework is hella weird
ohio type stuff
My current guess is that resource is causing the problem If it has functions in it, you can't pass it around components or any boundary with a $.
this is how i use it
https://srcb.in/6j1V7xnTC8
this is getUserResource
The error should definitely be better. If we can get a minimal repro, I'm sure the Qwik team would appreciate opening a bug
Also, when you have time to come back and work on this, I would recomend updating to the latest versions to see if it's a bug they've fixed
alright thanks for your help
@jagged crane I have figured out the problem
so basically i can access everything, but the only thing i cant access(even with a store) is props.resource
because resource is an object with a function insdie it
and we cant access functions
i think i just need to turn the function into a qrl
Yes, that sounds right
it still isnt working
functions just arent working
@jagged crane
[plugin:vite-plugin-qwik] Qrl($) scope is not a function, but it's capturing local identifiers: props
says this
const run = $(resource.run);
You probably need to wrap run in $ before you add it to resource
im not sure how would I do that
resource is an object with the run function, but i need the run function as a qrl
ok i have fixed it
one thing i have noticed
is that it doesnt neither allow functions or objects
and i needed to encode my object in base64 before having to access it, idk why qwik is built like this
its annoying to have to encode my objects to use them in a qrl
I've never had problems with objects unless the object includes a function or class or something else that isn't serializable.
There's reasons for these limitations, but it's still super unfortunate that devs have to work around these rough edges. I'm not sure how widely Qwik can be adopted if they don't improve these things
Oh i just figured it out
the object has a function inside it too π
i totally forgot about that
One "escape hatch" is noSerialize. You can wrap anything in that function and Qwik won't try to serialize it. The downside is that it might become undefined unexpectedly at times when it would normally be serialized. noSerialize should only be a last resort