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

Reactive Variables

Guide to Reactive State in Svelte

Svelte is a modern JavaScript framework that makes it easy to build reactive web applications. One of its powerful features is the ability to declare reactive state and derived state. This guide will walk you through how to use these features effectively.

Basic Reactive State

In Svelte, you can declare a piece of reactive state using the $state rune. This allows your application to automatically update when the state changes.

Example Code

Here is a minimal example of declaring and using reactive state in Svelte:

<script>
    let name = $state('Shento');
</script>

<h1>My name is {name}</h1>

My name is Shento

Derived State

If a value depends on another value, you can use the $derived rune. This ensures that the derived value is automatically recalculated whenever the dependent value changes.

Example Code

Let's extend our previous example to include a derived state:

<script>
    let name = $state('Shento');
    let uppercaseName = $derived(name.toUpperCase());
</script>

<h1>My name is {uppercaseName}</h1>

My name is SHENTO

Explanation

Let's break down the code:

  • Reactive State Declaration:

    let name = $state('Shento');

    Here, name is a reactive state variable initialized with the value 'Shento'. You can name this variable anything you like.

  • Derived State Declaration:

    let uppercaseName = $derived(name.toUpperCase());

    uppercaseName is a derived state that depends on name. It automatically updates to the uppercase version of name whenever name changes. The .toUpperCase() method is standard JavaScript.

  • HTML Binding:

    <h1>My name is {uppercaseName}</h1>

    This line binds the uppercaseName variable to the HTML, so it displays the current value of uppercaseName.

Important Points

The $state and $derived runes are specific to Svelte and are used to create reactive and derived state variables.

Make sure to always use the $derived rune for values that depend on other reactive state variables to ensure they update correctly.

Summary

  • Use $state to declare reactive state variables.

  • Use $derived to declare variables that depend on other reactive state variables.

  • Bind these variables to your HTML to automatically update the UI when the state changes.

PreviousCurly Braces & Understanding Core SyntaxNextTwo-way-binding Shortcut

Last updated 9 months ago