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

Handling potential null external svelte 5 state

$
0
0

I have an external state with an exported getter in file.svelte.ts:

let data: T | null = $state(null);export function get_data() {    return data;}

A component should use this state, however the component's logic does not allow for the state to be null.

So, I tried $derived.by:

<script lang="ts">    import { navigate } from "svelte-routing";    import { get_data } from "file.svelte";    let data: T = $derived.by(() => {        let external = get_data();        if (!external) navigate("/null-error-page");        return external;    });</script>

This gives off a typescript error because a navigate() call does not terminate the function's execution. How can i call navigate() such that it completely stops the loading of the component?

EDIT: I can!t just move the logic into an onMount call, i need the component to call navigate() whenever that external state is set to null.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>