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

Working with Javascript and Rune reactivity

With runes, reactivity extends beyond the boundaries of your .svelte files.

<script>
	import { createCounter } from './counter.svelte.js';
	
	const counter = createCounter();
</script>

<button on:click={counter.increment}>
	clicks: {counter.count}
</button>
export function createCounter() {
	let count = $state(0);

	return {
		get count() { return count },
		increment: () => count += 1
   };
}

counter.svelte.js

In counter.svelte.js we created a reactive piece of state called count using $state(0) and it returns two usable functions:

get count() { return count }
  • This is a getter function for the count property.

  • When you access the count property on the returned object, this getter function is invoked, and it returns the current value of the count state.

  • This allows you to retrieve the current value of count as if it were a regular property, but behind the scenes, it is dynamically pulling the latest reactive state.

increment: () => count += 1
  • This is an arrow function that increments the value of count by 1.

  • Since count is reactive, any part of your Svelte component that depends on count will automatically update when this method is called.

App.svelte

First the javascript code is imported, and assigned to a variable. Then we can access those returned functions using the dot . to access them.

Outside .svelte components, runes can only be used in .svelte.js and .svelte.ts modules.

PreviousUsing $effectNextWorking with Conditionals & Loops

Last updated 9 months ago