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

Discussion: Allow <svelte:self> as slot without if/each block? #2798

Closed
trbrc opened this issue May 17, 2019 · 4 comments
Closed

Discussion: Allow <svelte:self> as slot without if/each block? #2798

trbrc opened this issue May 17, 2019 · 4 comments

Comments

@trbrc
Copy link
Contributor

trbrc commented May 17, 2019

The API docs say the following about <svelte:self>:

It cannot appear at the top level of your markup; it must be inside an if or each block to prevent an infinite loop.
https://svelte.dev/docs#svelte_self

However, there is at least one case where unbounded recursion can be prevented, outside of #if/#each, which is when <svelte:self> is used as a slot in another component. That component may expose the slot conditionally in a way that restricts recursion depth.

See REPL for a silly example that would run if the compiler allowed it: https://svelte.dev/repl/6df2eaa40872455d9ab9487351687281?version=3.4.1

Here I've wrapped it in a pointless if just to appease the compiler:
https://svelte.dev/repl/bca90979b8e444cdb05b13969cb6e601?version=3.4.1

If the purpose is to prevent guaranteed infinite loops, rather than risk of infinite loops, the compiler should arguably allow a pattern like this too.

@varholak-peter
Copy link
Contributor

That is true, but at the same time it tightly couples the implementation of the other component to one with <svelte:self />.
If someone comes later and decides to change the other component to exclude the if later on, it might break the app in multiple places. Ultimately this moves the responsibility from parent to a child which isn't the right approach in terms of reusability.

@trbrc
Copy link
Contributor Author

trbrc commented May 17, 2019

I don't know, that feels a little like a rationalization of the current behavior. Is the limitation there to enforce a certain architecture, or to help someone along when they create an obvious infinite loop?

@varholak-peter
Copy link
Contributor

I think it's more about preventing accidental infinite loop. When you build an app and integrate an infinite loop you can pretty much see that something broke when you open your browser and it gets stuck. However, when you are refactoring a greater codebase you don't want to be constantly thinking about where the children are used throughout the app and removing that inside #if can break other parts which may not really be obvious.

@Conduitry
Copy link
Member

In 3.20.0, you can now use <svelte:self> in a slot. https://svelte.dev/repl/6df2eaa40872455d9ab9487351687281?version=3.20.0

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