-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Reusable HTML snippets (or svelte's answer for React's arrow function components) #3869
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
Comments
I'm pretty sure you can already do this with a preprocessor. Write one that looks for type="scopedComponent" and replaces all the instances of it in the code. Personally I don't really see the point of this. I would just make another file and spread the props on each instance. Like so: |
As you mention yourself, the best alternative is to make the repeated part it's own component.
This can be done with a simple spread operator as |
In your example, all the blocks inside your if / else if statements are the same. Only the final else block is different. If that's your actual use case, why not do all that logic in the script block at the top? |
thanks for your comments! the The value here is the same as having private methods in OO languages or arrow functions in React. Both features are popular and useful for reducing code complexity and overhead in many cases, though the language/framework core design pattern is a module (class/component). |
If you must have this, write a preprocessor that does this: https://svelte.dev/docs#svelte_preprocess I don't think it's super complicated :) |
I ended up hacking up something when I needed to do this. (not advised) <AsComponent bind:component={template}>
<div>
<img src={src} alt="test image"/>
</div>
</AsComponent> then use like <svelte:component this={template} /> |
@david-pershouse - looks good at first glance, thanks! I'll check it out! |
It relies on svelte internals so it could break at any time. In my case I didn't have a choice |
This feature is going to be implemented with this RFC: sveltejs/rfcs#34 |
Is your feature request related to a problem? Please describe.
Yes! I'm frustrated that we can't reuse simple html code within a single component file, to use internally by this component alone (i.e. "arrow function components" in React)...
consider the following component:
Since the
<div><img .../></div>
snippet is reused, my code is now bloated with boilerplate 😞 .Describe the solution you'd like
type="scopedComponent"
and string template is just a made-up API, don't take it as a suggestion...Describe alternatives you've considered
Writing an additional
.svelte
for the above, But it has its own cost. For example, the reused html requires many props, I need the root component passing them again and again which bloats my code again...How important is this feature to you?
My team develop a huge-scale and mission critical app based on svelte. Without a simple and elegant solution for reusing code within the same component we're gonna have tons of redundant code.
The text was updated successfully, but these errors were encountered: