
- Svelte - Home
- Svelte - Introduction
- Svelte - Installation
- Svelte - Reactivity
- Svelte - Props
- Svelte - Logic
- Svelte - Events
- Svelte - Bindings
- Svelte - Classes
- Svelte - Styles
- Svelte - Actions
- Svelte - Transitions
- Svelte - Advance Reactivity
- Svelte - Reusing Content
- Svelte - Motion
- Svelte - Advanced Binding
- Svelte - Advanced Transitions
- Svelte - Context API
- Svelte - Special Elements
- SvelteKit Basics
- SvelteKit - Introduction
- SvelteKit - Routing
- SvelteKit - Loading Data
- SvelteKit - Headers
- SvelteKit - Cookies
- SvelteKit - Shared Modules
- SvelteKit - Forms
- SvelteKit - API Routes
- SvelteKit - States
- SvelteKit - Errors
- SvelteKit - Redirects
- SvelteKit Advanced
- SvelteKit - Hooks
- SvelteKit - Page Options
- SvelteKit - Link Options
- SvelteKit - Advanced Routing
- SvelteKit - Advanced Loading
- SvelteKit - Environment Variables
- Svelte Resources
- svelte - Useful Resources
- svelte - Discussion
Svelte - Props
Props in Svelte are a powerful feature that allows for the creation of dynamic and reusable components. They enable a clear and manageable data flow from parent to child components. By using props effectively, you can build complex applications while keeping your code organized and maintainable.
What are Props?
Props is short form for properties are a mechanism to pass data from a parent component to a child component. This feature of svelte allows you to create reusable and dynamic components, making your applications more modular and efficient.
Declaring Props in Svelte
To effectively use or declare props in Svelte, follow these steps:
- Create a Child Component: Firstly, have to create a child component that will accept the prop form the parent component. For example: Child.svelte.
- Define the Prop in the Child Component: Use the export keyword to define the prop in the child component. This allows the parent component to use it.
<script> export let userName; // Declare a prop named 'userName' </script>
<script> export let userName; // Declare a prop named 'userName' </script>
<p>Hey there! Welcome to</p> <h1>{userName}</h1>
Using Default Values in Props
Svelte allows you set default values for props. If the parent doesn't provide a prop, the child will use this default value.
<script> export let userName= 'Tutorialspoint'; // Default value </script>
Spreading Props
When you have an object with several props, you can use the spread operator to send those props into a component. For scenarios where multiple props are available, spread allow us to use those props simultaneously as a single object.
<script> import Child from './Child.svelte'; let childBox ={ userName: "Tutorialspoint", country: "India's", company: "EdTech Company" } </script> <main> <Child {...childBox} /> </main>
Example
The following example is illustrating the use of spreading props in svelte.
<!--App.svelte--> <script> import Child from './Child.svelte'; let childBox ={ userName: "Tutorialspoint", country: "India's", company: "EdTech Company" } </script> <main> <Child {...childBox} /> </main> <!--Child.svelte--> <script> export let userName; // Declare the prop export let country; export let company; </script> <p>Hey there! Welcome to</p> <h1>{userName}</h1> <h2>{country} best {company}</h2>
Output
