xoidlabs / xoid

Framework-agnostic state management library designed for simplicity and scalability ⚛
https://xoid.dev
MIT License
158 stars 7 forks source link

`useSetup()` isn’t recognized as a setup context for an `effect()` #30

Closed awgv closed 6 months ago

awgv commented 6 months ago

Hello,

In the context of feeding an external callback to useSetup() like so:

https://github.com/xoidlabs/xoid/blob/728147e93318f8a186b2fe79dbf958fa2009d59d/examples/finite-state-stopwatch/src/App.tsx#L6

https://github.com/xoidlabs/xoid/blob/728147e93318f8a186b2fe79dbf958fa2009d59d/examples/finite-state-stopwatch/src/App.tsx#L41

xoid throws an error if you use an effect() in an external callback, although, the following (also blows up using the latest release 1.0.0-beta.12) suggests that it should work:

https://github.com/xoidlabs/xoid/blob/728147e93318f8a186b2fe79dbf958fa2009d59d/examples/framework-agnostic-library/core.tsx#L1-L18

The docs mention this non-existent signature—what’s up with that?

+useSetup(($props, { effect }) => {
  effect(() => {
    […]
  })
}, props)

Thank you!

onurkerimov commented 6 months ago

Hi @awgv ! Thank you for reporting.

The docs mention this non-existent signature—what’s up with that?

Thank you for pointing this out! Apparently I forgot to update this part of the docs. It was the old way to use effect and inject when they were still experimental.

xoid throws an error if you use an effect() in an external callback, although, the following (also blows up using the latest release 1.0.0-beta.12) suggests that it should work:

I couldn't understand this part. I have just tested it manually, and it using effect in useSetup's callback function works. This is intended. Are you saying that is shouldn't work according to docs?

awgv commented 6 months ago

Thanks for clearing that up.

I couldn’t understand this part. I have just tested it manually, and it using effect in useSetup’s callback function works. This is intended. Are you saying that is shouldn’t work according to docs?

Disregard that—I went to create a CodeSandbox, but found some weirdness. So, locally, xoid throws an error for me when an effect() is used inside of a useSetup() and it doesn’t matter if a useSetup()‘s callback is external or local:

Error: [xoid] effect cannot be used outside the setup context. To create a setup context, use the useSetup hook from a framework integration package.

There’s no error on CodeSandbox, but the effect() doesn’t seem to have any actual effect. Could you please take a look at it and tell me if it’s the correct use of useSetup()/effect()? If I run exactly this locally, I get the aforementioned error—I’ll investigate.

onurkerimov commented 6 months ago

This is really interesting. Were you able to figure this out? I tried with different React versions, and after modifying the index.ts like this, effect starts working properly: https://codesandbox.io/p/sandbox/xoid-issue-30-forked-9gl629. (Using ReactDOM.render instead of the new react-dom/client API) I found this by trial and error. I have no idea what's going on. Locally, there's no difference, but in CodeSandbox there is. I want to investigate this more.

awgv commented 6 months ago

Thanks for taking your time to take a look.

Were you able to figure this out?

No, I’ve also been grasping at straws and trying completely random things to no avail. React’s deprecated API doesn’t cure local errors; checked with react@17.0.2 just in case. I’ll @ you in this thread if I’ll have more information.