Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from a layout to a page in Sapper?

Tags:

svelte

sapper

In Svelte you can pass props to the content in a slot.

<Component let:message="Hello!">
    <div>
        {message}
    </div>
</Component>

When Sapper uses a layout to render a route, the route contents are rendered in a slot too. The problem is that since Sapper takes control of this process it doesn't seem possible to pass a slot prop to a route.

This doesn't work:

// _layout.svelte
<slot message="Hello!"></slot>

So what is the appropriate way of passing data from a layout to a rendered route? For example segment.

like image 598
Pier Avatar asked Mar 03 '23 15:03

Pier


1 Answers

Pass it down through context.

One thing to be aware here is that a _layout component won't be recreated when the route change. And Svelte's context is not reactive. Conclusion: you need to wrap your data in a store.

_layout.svelte

<script>
  import { setContext } from 'svelte'
  import { writable } from 'svelte/store'

  export let segment;

  const segment$ = writable(segment)

  // this updates the store's value when `segment` changes
  // syntactic sugar for: segment$.set(segment)
  $: $segment$ = segment

  setContext('segment', segment$)
</script>

<slot />

In some child page of this layout:

<script>
  import { getContext } from 'svelte'

  const segment$ = getContext('segment')

  $: segment = $segment$

  $: console.log(segment)
</script>

like image 52
rixo Avatar answered Mar 17 '23 02:03

rixo