Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • Setting up props
  • Children prop
  1. Getting Started with SvelteKit
  2. Base Syntax & Core Features of Svelte

Components & Communication via Props

Setting up props

In Svelte, you can give component props (properties), these are attributes that can be used to communicate between a parent and child properties.

Child.svelte
<script>
    const {
    name = "",
    age = null,
    } = $props();
</script>

<h2>This is a child Component</h2>
<p>My name is {name} and I'm {age}</p>
Parent.svelte
<script>
    import Child from '$lib/Child.svelte';
    let name = "Shento";
    let age = 16;
</script>
<Child name={name} age={age} />

If the prop and value are the same like this:

<Child value={value} />

You can use a shorthand of <Child {value} />

Children prop

In Svelte 5, the concept of "children props" is a way to pass content or elements from a parent component to a child component. This feature is particularly useful when you want to create components that can be more dynamic and reusable by allowing them to render different content depending on what the parent provides.

<script>
  import MyButton from "./MyButton.svelte";
</script>

<FunnyButton>Click me!</FunnyButton>
<script>
  let { children } = $props();
</script>

<button>
  {@render children()} <!-- Click me! will appear here ->
</button>
PreviousUsing multiple componentsNextDisplay HTML in Svelte

Last updated 9 months ago