Svelte · Middle · Question 23 of 75

What does createContext (Svelte 5.40+) give you?

import { createContext } from "svelte";
export const [getUser, setUser] = createContext<User>();

Explanation

getUser() throws if no ancestor set the context, which catches mistakes early. Svelte recommends it over setContext/getContext.

Further reading

Problem with a question

Svelte · Middle · question 23

Test yourself

Take the Svelte · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz