Search code examples
sveltekit

How to make a global variable in sveltekit?


I need a global variable to store the current user state and token. But I can't understand how this is done in the latest version of SvelteKit. How can I make it visible from all components of the application


Solution

  • I would recommend returning that from a load function in the top-level +layout.js/ts. Then it will be part of every page data and also via the page store data, so you could use something like $page.data.user in any component.