#Qwik signals and useOnWindow

57 messages Β· Page 1 of 1 (latest)

cedar jolt
#

I cant access my signal inside the useOnWindow callback, how do i get around this? i need to use my signal there

jagged crane
cedar jolt
#

yes

jagged crane
#

I have a suspicion of what's wrong, but let me confirm before I lead you down a wrong track πŸ™‚

cedar jolt
#

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

jagged crane
jagged crane
#

That's right. I also didn't get the socketio stuff working though

cedar jolt
#

cuz there is my server code running on my machine on localhost

#

it doesnt matter tho i can still reproduce

jagged crane
#

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 use strategy: '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.

cedar jolt
#
"@builder.io/qwik": "^0.19.2",
    "@builder.io/qwik-city": "^0.4.0",
#

im a bit behind

jagged crane
#

I just changed to those versions and it's still working

cedar jolt
#

i dont know why but stuff like this just happens sometimes

#

this framework is hella weird

#

ohio type stuff

jagged crane
#

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 $.

jagged crane
#

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

cedar jolt
#

@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

jagged crane
#

Yes, that sounds right

cedar jolt
#

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);

jagged crane
#

You probably need to wrap run in $ before you add it to resource

cedar jolt
#

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

jagged crane
#

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

cedar jolt
#

Oh i just figured it out

#

the object has a function inside it too πŸ’€

#

i totally forgot about that

jagged crane
#

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

cedar jolt
#

great, using last resorts is my first resort

#

@jagged crane Now i have 2 more problems, i have complete fixed resource and now it works in my useOnWindow, but my apiState and my resourceArgs arent working, resourceArgs is just an object, and apiState is my signal