tezvyn:

Svelte Context API: Avoid Prop-Drilling

Source: svelte.devintermediate

Svelte's Context API lets a parent component provide data to any descendant, avoiding 'prop-drilling'. It's ideal for sharing app-wide state like user info or themes. The key footgun: calling `getContext` without a parent `setContext` will throw an error.

Svelte's Context API creates a direct communication channel from a parent to any descendant, eliminating 'prop-drilling' through intermediate components. It's ideal for sharing data like user info or UI themes within a component subtree. The new `createContext` function provides a type-safe `[get, set]` pair, which is preferred over the older key-based `setContext` and `getContext` functions. The main footgun: calling `getContext` without an ancestor having set the context will throw a runtime error.

Read the original → svelte.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Svelte Context API: Avoid Prop-Drilling · Tezvyn