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
  1. Getting Started with SvelteKit
  2. Base Syntax & Core Features of Svelte

Using multiple components

Svelte Component Cheat-Sheet

1. Create a Child Component

File: src/components/ChildComponent.svelte

<script>
  const { name } = $props();
</script>

<p>Hello, {name}!</p>

2. Use the Child Component in Main App Component

File: src/App.svelte

<script>
  import ChildComponent from './components/ChildComponent.svelte';
</script>

<main>
  <ChildComponent name="Svelte" />
</main>

3. Display Result in Browser

URL: localhost

Hello, Svelte!

Key Points

  • Exporting Props in Child Component:

    <script>
      const { name } = $props();
    </script>
    • const { name } = $props();: Svelte-specific syntax to allow prop passing.

  • Using Child Component in Parent:

    <script>
      import ChildComponent from './components/ChildComponent.svelte';
    </script>
    
    <main>
      <ChildComponent name="Svelte" />
    </main>
    • import: Standard JavaScript ES6 syntax.

    • <ChildComponent name="Svelte" />: Svelte-specific syntax for including a component and passing props.

The name ChildComponent can be any name you like. It is not specific to Svelte.

Ensure that the file path in the import statement is correct. Incorrect paths will result in errors.

PreviousTwo-way-binding ShortcutNextComponents & Communication via Props

Last updated 9 months ago