ajay dalwani
ajay dalwani

Reputation: 99

In sveltekit , how to pass props in slot using __layout.svelte File?

In __layout file I want to pass one variable lets say btnclicked="HR",and I want to collect same in the component which is rendered in Slot ? how to do it pls help ?

Upvotes: 9

Views: 4351

Answers (1)

EvanMorrison
EvanMorrison

Reputation: 1217

If you want to pass data that will be reactive, then I think the way to do it is to use a Svelte store with Context.

// in __layout.svelte
  <script>
    import { setContext } from 'svelte';
    import { writable } from 'svelte/store';

    const btnClicked = writable('');

    setContext('btnClicked', btnClicked);

    //...some code that sets the btnClicked store value
  </script>

  <slot />

// in child.svelte
  <script>
    import { getContext } from 'svelte';

    const btnClicked = getContext('btnClicked');

    // use $btnClicked in this component to access the value
  </script>

Upvotes: 2

Related Questions