Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How do I use $state inside hooks.client.ts?

$
0
0

I'm trying to implement authentication with Pocketbase and Svelte 5, following this repo.

In $lib/stores/user.svelte.ts I'm creating a $state store (not sure how to call it?):

// src/lib/stores/user.svelte.tsimport { type AuthModel } from 'pocketbase'export let currentUser: AuthModel | null = $state(null)

In the hooks.client.ts I'm supposed to watch for changes in Pocketbase's auth store, but since $state doesn't work in .ts files I figured I should move that code in +layout.svelte:

// src/+layout.svelte<script lang="ts">    import { pb } from '$lib/pocketbase'    import { currentUser } from '$lib/stores/user.svelte'    let { children } = $props()    pb.authStore.loadFromCookie(document.cookie)    pb.authStore.onChange(() => {        currentUser = pb.authStore.model        document.cookie = pb.authStore.exportToCookie({ httpOnly: false })    }, true)</script>{@render children()}

If I understand correctly, $state works only in .svelte and .svelte.ts files and it should make currentUser behave like a normal variable. According to the docs, reassignment should work too. However, in VSCode I'm getting:

Cannot assign to import svelte(constant_assignment)


Viewing all articles
Browse latest Browse all 1541

Trending Articles