Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

effect_orphan $effect can only be used inside an effect #15332

Open
sacrosanctic opened this issue Feb 18, 2025 · 4 comments
Open

effect_orphan $effect can only be used inside an effect #15332

sacrosanctic opened this issue Feb 18, 2025 · 4 comments

Comments

@sacrosanctic
Copy link
Contributor

sacrosanctic commented Feb 18, 2025

Describe the bug

Difficult to understand error msg.

Reproduction

REPL

Related

https://discord.com/channels/457912077277855764/1341461743305494609

Severity

annoyance

@Conduitry
Copy link
Member

What would you suggest? When something's imported like that, it's just run once, outside of the initialization of any particular component. You'd have to put the $effect() inside a function call, and then call that from the top level of your component's <script> to ensure that it's getting run during the initialization of a specific component.

@Ocean-OS
Copy link
Contributor

I do think we should better explain $effect.root in the documentation as to provide more insight to these errors. I don't think we should be dependent on people reading documentation, but if they are confused they'll probably consult it. We could take inspiration from Solid's createRoot docs to improve our info on effect roots.

@sacrosanctic
Copy link
Contributor Author

sacrosanctic commented Feb 19, 2025

There's no obvious corrective action for the user to take. The error msg is almost cyclical. Clicking through the url leaves them with the same error msg.

Image

Users expect that to work because they can do this

// foo.svelte.js
// This works
export const foo = $state({value: 1})

// So they think this works, mirroring a *.svelte component
$effect(()=>{
  conole.log('here')
})

I think defining inside an effect or provide an example would go a long way to resolve this confusion.

@sacrosanctic
Copy link
Contributor Author

https://discord.com/channels/457912077277855764/1343686205694541905

effect_orphan $inspect can only be used inside an effect (e.g. during component initialisation) https://svelte.dev/e/effect_orphan

Here's another instance of someone using $inspect in <script module>. The error again doesn't guide the user in any way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants