question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reusable HTML snippets (or svelte's answer for React's arrow function components)

See original GitHub issue

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:

<script>
let id, src, ... // more props
</script>

{#if id === 'x'}
	<h1>I am X</h1>
	<div>
	  <img src={src} .../>
	</div>
{:else if id === 'y'}
	<h2>I am Y</h2>
	<div>
	  <img src={src} .../>
	</div>
{:else if id === 'z'}
	<h3>I am Z</h3>
	<div>
	  <img src={src} .../>
	</div>
{:else}
	<pre>not supported</pre>
{/if}

Since the <div><img .../></div> snippet is reused, my code is now bloated with boilerplate 😞 .

Describe the solution you’d like

<script>
let id, src, ... // more props
</script>

<script type="scopedComponent">
const MyImg = `
<div>
  <img src=${src} .../>
</div>
`
</script>

{#if id === 'x'}
	<h1>I am X</h1>
	<MyImg />
{:else if id === 'y'}
	<h2>I am Y</h2>
	<MyImg />
{:else if id === 'z'}
	<h3>I am Z</h3>
	<MyImg />
{:else}
	<pre>not supported</pre>
{/if}

  • the type="scopedComponent" and string template is just a made-up API, don’t take it as a suggestion…
  • notice that the scoped component can access the props directly, that is also necessary.

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
david-pershousecommented, Nov 7, 2019

I ended up hacking up something when I needed to do this. (not advised) https://svelte.dev/repl/4531f96e7f8a4d40adfdfd3a4f379893?version=3.12.1

<AsComponent bind:component={template}>
	<div>
	  <img src={src} alt="test image"/>
	</div>
</AsComponent>

then use like

<svelte:component this={template} />
0reactions
raythurnevoidcommented, Sep 28, 2020

This feature is going to be implemented with this RFC: https://github.com/sveltejs/rfcs/pull/34

Read more comments on GitHub >

github_iconTop Results From Across the Web

Functions — reusable blocks of code - Learn web development
In this article we'll explore fundamental concepts behind functions such as basic syntax, how to invoke and define them, scope, and parameters.
Read more >
Arrow Function Snippets - Visual Studio Marketplace
The arrow function allows to accomplish the same result with fewer lines of code and approximately half the typing. Curly brackets aren't required...
Read more >
[AskJS] why are arrow functions used so universally nowdays ...
I've never really understood this: why would you use a `const` here and an arrow function when you can just use the function...
Read more >
React Function Components
When using arrow functions for React components, nothing changes for the props. They are still accessible as arguments as before. It's a React...
Read more >
Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. ... This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found