Svelte · Middle · Question 22 of 75

How do you make context reactive?

<script>
  import { setContext } from "svelte";
  let user = $state({ name: "Ann" });
  setContext("user", user);
</script>

Explanation

Reassigning the variable user = {...} doesn't update context, because the context holds the original object.

Further reading

Problem with a question

Svelte · Middle · question 22

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