Skip to content

How to use createRawSnippet to test components? #13518

Answered by 7nik
unikitty37 asked this question in Q&A
Discussion options

You must be logged in to vote

render() should return an ordinary HTML, while in setup() you make it interactive (add event listeners or mount a component).

Usage is like this:

<script>
	import { createRawSnippet, mount, unmount } from 'svelte';
	import Component from "./Component.svelte";
	import Container from "./Container.svelte"

	const props = $state({ name: "Test" })
	
	const snip = createRawSnippet((name) => {
		return {
			render: () => `<div></div>`,
			setup: (target) => {
				const comp = mount(Component, { target, props }); 
				return () => unmount(comp);
			}	
		};
	});
</script>

<input bind:value={props.name}><br>
<Container children={snip} />

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@7nik
Comment options

@unikitty37
Comment options

@ar4hc
Comment options

@7nik
Comment options

@7nik
Comment options

Answer selected by unikitty37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants