Back to Calendar

Day 21 createContext

Santa's naughty-and-nice list app needs notifications — but threading a notification function through every layer of components is like untangling a mile of Christmas lights! That's where createContext comes in.

The old way was a bit of a ceremony: import getContext and setContext from Svelte, create a Symbol to avoid string collisions, then export typed wrapper functions just to get proper TypeScript support. Ho-ho-hold my eggnog — there's a better way!

With createContext, you call it once with your type, and it hands you back a tuple of [getContext, setContext] functions, already typed and ready to use. No symbols, no strings, no boilerplate. In your layout, set the notification function; in any deeply nested component, call getContext() and it just works!

It's like Santa having a direct hotline to every elf in the workshop — no matter how deep in the toy factory they are. When Rich Harris gets added to the nice list, that notification pops up at the top of the screen instantly!

Read the docs

Credits

Illustrations by Icons 8:

  • Santa Hat
  • Santa sleigh
  • Cookie
  • Heart
  • Santa face
  • Present
  • ID
  • Snowflake
  • Snow Globe
  • Question Mark
  • Fairy Lights
  • Piggy Bank
  • Globe
  • Present #2

Illustrations by Elisabet Guba:

  • Clock

Icons by Obra Icons

Challenges by Svelte Ambassadors:

  • Stanislav Khromov
  • Willow (GHOST)
  • Paolo Ricciuti

Santa Tracker Data by NORAD via Danielle Vass