S
SolidJS14mo ago
Davide

Context undefined when called from a function in onClick, but not if called in component

I have a component which has a button that onClick calls a function. That function tries to access a few contexts but they are all undefined. If I try to access the contexts from the caller component they work. Is there a specific reason why this happens? Any workarounds aside from passing down all the contexts to the function every time?
10 Replies
Davide
DavideOP14mo ago
you can easily reproduce the issue here
Davide
DavideOP14mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
fabiospampinato
fabiospampinato14mo ago
the context is only visible inside children computations where "inside" means you are executing code after the computation got called/refreshed, and before it ended the onClick callback is executing at a later point in time, outside of any computations so it sees no contexts I don't know if there's a nice primitive in #solid-primitives for this, it could be interesting you can solve your problem with runWithOwner
Davide
DavideOP14mo ago
ive never heard of that, is it documented somewhere?
fabiospampinato
fabiospampinato14mo ago
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
fabiospampinato
fabiospampinato14mo ago
runWithOwner+getOwner and you can execute stuff inside whatever computation you can get yours hands on
Davide
DavideOP14mo ago
so in this case the owner should be the parent component which is calling the function?
fabiospampinato
fabiospampinato14mo ago
components are not really owners, owners are things like memos, effects and roots getOwner gives you the current owner, so you can just call it inside your component
Davide
DavideOP14mo ago
it works I wonder if there is a way to abstract this from the caller or does the component caller need to know to run it with runWithOwner every time?
fabiospampinato
fabiospampinato14mo ago
It could be abstracted away at Solid's level, but it isn't free, so not worth it probably
Want results from more Discord servers?
Add your server