Phong Duong


Props in Svelte

04-08-2020 | #svelte

To define a property(prop) in Svelte, you use export keyword and export the variable that used as a prop. Your variable should be instantiated with let keyword. If you export const, classor function, it is read only. You can export function using function expression. When instantiating a prop, it is recommended to assigned a default value

	export let bar = 'optional default initial value';
	// these are readonly
	export const thisIs = 'readonly';

	export function greet(name) {
		alert(`hello ${name}!`);

	// this is a prop
	export let format = n => n.toFixed(2);

You can use reversed words as prop name

	let className;

	// creates a `class` property, even
	// though it is a reserved word
	export { className as class };

If you prefer learning by watching, you can watch this tutorial here

Share: TwitterFacebookLinkedInHacker News