| | |

Svelte 59 ๐Ÿงก Reading Loaded Data in Components via $props() Rune

โฑ๏ธ Estimated read: 8 min | |

Svelte 5 marks a shift toward explicit, fine-grained reactivity. Previously, Svelte components consumed data from load functions via the export let data pattern. Svelte 5 introduces the $props() rune, which standardizes how components receive data, including route data, layout data, and standard component inputs. This change is not merely cosmetic; it aligns data consumption with the modern reactivity model, enabling better type inference, cleaner component APIs, and a more predictable data flow across your application.

This chapter covers the transition from legacy export let patterns to the $props() rune. We will explore how to destructure data passed from server or universal loaders, how to maintain reactivity on sub-properties, and the role of TypeScript in defining the shape of your data object. Moving to $props() is the final step in ensuring your Svelte 5 application is fully compliant with the latest reactive standards.

Key point: The $props() rune is the modern, reactive way to declare component inputs in Svelte 5; when used in pages or layouts, it replaces export let data to receive the output of load functions, providing improved type safety and consistent syntax with other component properties.


Why exists

The Implicit Reactivity Problem. Legacy export let patterns relied on compiler magic that sometimes made it difficult for IDEs and type checkers to track component inputs. $props() exists to make the component interface explicit, allowing for better tooling support and stricter TypeScript integration.

The Standardization Problem. Svelte previously had different ways to declare props versus page data. $props() exists to provide a unified interface; a component receives route data, layout data, and parent-passed props using the exact same syntax, reducing cognitive load.

The Fine-Grained Update Problem. Older patterns often triggered component-wide updates. $props() is designed to work with Svelte 5โ€™s fine-grained reactivity, allowing the framework to pinpoint exactly which parts of a component need to update when specific data properties change.

The Destructuring Limitation. Destructuring export let variables often caused reactivity to break because the compiler couldn’t track the reference correctly. $props() is built specifically to support modern JavaScript destructuring patterns while maintaining deep reactive links to the underlying data.

The Alignment with Modern JS. Svelte 5 aims to be closer to native JavaScript. By using a function call ($props()) to capture inputs, Svelte aligns with modern reactive framework trends, making the code more readable for developers coming from other environments while retaining the Svelte philosophy.


a. Basic syntax and usage

In Svelte 5, you no longer export variables to receive data. Instead, you assign the result of the $props() function to a variable, typically using object destructuring. This variable acts as a reactive container for all inputs passed to the component from its parent or the SvelteKit router.

<script>
  // Receiving the 'data' object from the route loader
  let { data } = $props();
</script>

<h1>{data.user.name}</h1>

This syntax is consistent for both +page.svelte and +layout.svelte files. When SvelteKit resolves a route, it injects the data returned from your load function directly into the $props() object. If you have multiple pieces of dataโ€”for instance, if you passed a custom property from a parent componentโ€”it will also appear inside this same object.


b. Destructuring and Reactivity

One of the most significant advantages of $props() is that it handles destructuring more gracefully than legacy syntax. You can pull specific fields out of your data object immediately, and because Svelte 5 uses proxies for reactivity, updates to those specific sub-properties will still trigger the necessary DOM changes.

<script>
  let { data } = $props();
  // Destructuring into local variables
  let { title, content, author } = data.post;
</script>

<article>
  <h2>{title}</h2>
  <p>By {author}</p>
  <div>{@html content}</div>
</article>

This approach is highly idiomatic in Svelte 5. By destructuring early, your template logic remains clean and readable. Because $props() is reactive, if the load function runs again (e.g., during a client-side navigation update), the destructured variables will automatically update if the proxy-tracked properties change, maintaining the reactive contract without further boilerplate.


c. TypeScript and Typed Props

Because $props() returns a standard JavaScript object, TypeScript can infer the shape of your data automatically. In SvelteKit, this is typically handled via the generated $types.d.ts files. You can type your $props() by providing an interface or using the types exported by your load functions.

<script lang="ts">
  import type { PageData } from './$types';

  let { data }: { data: PageData } = $props();
</script>

By providing this type hint, you gain full IntelliSense support for your route data. If your load function returns an object with a user field, TypeScript will flag an error in your template if you attempt to access data.userr (a typo). This tight integration ensures that the most common source of runtime bugsโ€”mismatched data structures between server and clientโ€”is caught at compile time.


Complete Example Session

// ============================================
// PART 1: THE SERVER LOADER
// ============================================
// src/routes/profile/+page.server.ts
export const load = () => ({ 
  user: { name: "Alice", email: "a@ex.com" } 
});

// ============================================
// PART 2: THE COMPONENT (SVELTE 5)
// ============================================
// src/routes/profile/+page.svelte
<script>
  let { data } = $props();
</script>
<p>{data.user.name}</p>

// ============================================
// PART 3: NESTED DATA DESTRUCTURING
// ============================================
<script>
  let { data } = $props();
  let { name } = data.user;
</script>
<p>{name}</p>

// ============================================
// PART 4: TYPE ANNOTATION
// ============================================
<script lang="ts">
  import type { PageData } from './$types';
  let { data }: { data: PageData } = $props();
</script>

// ============================================
// PART 5: MULTIPLE PROPS
// ============================================
// Parent usage: <Profile {data} theme="dark" />
// Profile.svelte: let { data, theme } = $props();

// ============================================
// PART 6: FALLBACK VALUES
// ============================================
// let { data = { user: {} } } = $props();

// ============================================
// PART 7: REACTIVITY CHECK
// ============================================
// data.user.name = "Bob" // Re-renders in UI

// ============================================
// PART 8: LAYOUT INTEGRATION
// ============================================
// src/routes/+layout.svelte
// let { data, children } = $props();

// ============================================
// PART 9: CHILDREN SLOT
// ============================================
// {@render children()} (Replacement for <slot/>)

// ============================================
// PART 10: SVELTE 5 MIGRATION
// ============================================
// Replace 'export let data' with 'let { data } = $props()'

The example flow demonstrates the migration from legacy exports to the reactive $props() rune, showing how it supports typing, nested destructuring, and layout children.


Quick Reference

Syntax Comparison

FeatureLegacy Svelte (4)Svelte 5
Prop Declarationexport let xlet { x } = $props()
Page Dataexport let datalet { data } = $props()
Children Slot<slot />{@render children()}
Typingexport let x: typelet { x }: { x: type } = $props()

Data Flow

SourceAccess Method
load() returndata field in $props()
Parent ComponentNamed key in $props()
Defaults$props() destructuring default

Best Practices

โœ… Do This:

// Destructure for cleaner template access
let { data } = $props();                                    // โœ…

// Always type your props in TS
let { data }: { data: PageData } = $props();                // โœ…

// Use fallback values in destructuring
let { data = { user: {} } } = $props();                    // โœ…

// Use explicit naming for layout children
let { children } = $props();                                // โœ…

โŒ Don’t Do This:

// Don't mix legacy export with $props
export let data; let { props } = $props();                 // โŒ

// Don't mutate props directly (they are read-only)
data.user = "Bob"; // (Use state runes instead)            // โŒ

// Don't forget the type annotation in large files
// It helps IDEs understand your structure                 // โŒ

// Don't use <slot/> in Svelte 5
// Use the 'children' prop pattern                         // โŒ

Common Pitfalls

PitfallWhy It HappensFix
Data UndefinedNot destructured correctlyCheck $props() structure
Reactivity lossAssigning props to new varAlways use $props()
Slot errorUsing <slot />Use {@render children()}
Typing errorMismatched PageDataImport $types

Real-World Examples

1. Basic page data

<script>
  let { data } = $props();
</script>

2. Layout with children

<script>
  let { data, children } = $props();
</script>
{@render children()}

3. Typed props

<script lang="ts">
  let { data }: { data: PageData } = $props();
</script>

4. Default values

<script>
  let { title = "Default" } = $props();
</script>

5. Destructuring user data

<script>
  let { data } = $props();
  let { email } = data.user;
</script>

6. Layout props

<script>
  let { data, children } = $props();
</script>
<nav>{data.siteName}</nav>
{@render children()}

7. Reactive alias

<script>
  let props = $props();
  let data = $derived(props.data);
</script>

8. Required props

<script>
  let { requiredKey } = $props();
</script>

9. SvelteKit route data

<script lang="ts">
  import type { PageData } from './$types';
  let { data }: { data: PageData } = $props();
</script>

10. Complex prop object

<script>
  let { config } = $props();
</script>

Visual

The Prop Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Parent/Router                       โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  Data Object                         โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  $props() Receiver โ”€โ”€โ–บ Proxy Object  โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  Reactive Component UI               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Svelte 5 Destructuring Pattern

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  let { data } = $props()             โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  let { user } = data                 โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  User name: {user.name}              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Layout Tree Access

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Layout                              โ”‚
โ”‚  let { data, children } = $props()   โ”‚
โ”‚       โ”‚                              โ”‚
โ”‚  {@render children()}                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Rune$props()
AccessObject Destructuring
Types$types (PageData/LayoutData)
Layout Children{@render children()}
ReactivityProxy-based

Key takeaways:

  • Standardize on $props(). Replace all legacy export let declarations to ensure your components are compatible with Svelte 5’s reactivity model.
  • Embrace destructuring. Using object destructuring directly from $props() is the idiomatic way to expose your component’s data to the template.
  • Use TypeScript. Always type the $props() return value using the generated PageData or LayoutData types to catch bugs early in development.
  • Shift to children slots. If you are building a layout or a wrapper, accept a children prop and render it using {@render children()} instead of the deprecated <slot /> tag.
  • Read-only contract. Remember that props received via $props() are read-only. If you need to mutate state based on props, use $state or $derived runes to create local reactive mirrors.

Remember: The $props() rune is more than just a syntax change; it is the foundation for Svelte 5’s improved reactivity and type-safe architecture. By making your component’s inputs explicit, you enable your tooling to work for you, reducing runtime errors and creating a more predictable data flow across your entire application.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!