Skip to main content

We can easily specify default values for props in Nested.svelte:

<script>
	export let answer = 'a mystery';
</script>

If we now add a second component without an answer prop, it will fall back to the default:

<Nested answer={42} />
<Nested />
App.svelte
Nested.svelte
<script>
import Nested from './Nested.svelte';
</script>

<Nested answer={42} />

/* App.svelte generated by Svelte v4.2.19 */
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
noop,
safe_not_equal,
transition_in,
transition_out
} from "svelte/internal";

import "svelte/internal/disclose-version";
import Nested from './Nested.svelte';

function create_fragment(ctx) {
let nested;
let current;
nested = new Nested({ props: { answer: 42 } });

return {
c() {
create_component(nested.$$.fragment);
},
m(target, anchor) {
mount_component(nested, target, anchor);
current = true;
},
p: noop,
i(local) {
if (current) return;
transition_in(nested.$$.fragment, local);
current = true;
},
o(local) {
transition_out(nested.$$.fragment, local);
current = false;
},
d(detaching) {
destroy_component(nested, detaching);
}
};
}

class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, null, create_fragment, safe_not_equal, {});
}
}

export default App;
result = svelte.compile(source, {
generate:
css:
});
/* Add a <style> tag to see compiled CSS */