Svelte · Middle · Question 67 of 75
How do you check whether a context exists before reading it?
- ✓Correct answer: With
hasContext(key)fromsvelte - 2
getContextreturnsfalseif missing - 3With
try/catcharoundsetContext
Explanation
getContext returns undefined for a missing key; getAllContexts returns the whole map, e.g. to pass it to mount.