From b1cbac1a9ebe018b1e0bff51edf4b38b475f1e12 Mon Sep 17 00:00:00 2001 From: leno23 <39647285+leno23@users.noreply.github.com> Date: Wed, 18 Mar 2026 02:04:20 -0400 Subject: [PATCH 1/2] docs: recommend createContext as the primary method for context --- documentation/docs/06-runtime/02-context.md | 78 ++++++++++++++------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/documentation/docs/06-runtime/02-context.md b/documentation/docs/06-runtime/02-context.md index 61b203f93e..87bb0db843 100644 --- a/documentation/docs/06-runtime/02-context.md +++ b/documentation/docs/06-runtime/02-context.md @@ -2,28 +2,46 @@ title: Context --- -Context allows components to access values owned by parent components without passing them down as props (potentially through many layers of intermediate components, known as 'prop-drilling'). The parent component sets context with `setContext(key, value)`... +Context allows components to access values owned by parent components without passing them down as props (potentially through many layers of intermediate components, known as 'prop-drilling'). + +The recommended way to use context is with `createContext`, which provides type safety and makes it unnecessary to use a key: + +```ts +/// file: context.ts +// @filename: ambient.d.ts +interface User { + name: string; +} + +// @filename: index.ts +// ---cut--- +import { createContext } from 'svelte'; + +export const [getUserContext, setUserContext] = createContext(); +``` + +In your parent component, you can then set the context: ```svelte ``` -...and the child retrieves it with `getContext`: +...and the child retrieves it: ```svelte -

{message}, inside Child.svelte

+

hello {user.name}, inside Child.svelte

``` This is particularly useful when `Parent.svelte` is not directly aware of `Child.svelte`, but instead renders it as part of a `children` [snippet](snippet) ([demo](/playground/untitled#H4sIAAAAAAAAE42Q3W6DMAyFX8WyJgESK-oto6hTX2D3YxcM3IIUQpR40yqUd58CrCXsp7tL7HNsf2dAWXaEKR56yfTBGOOxFWQwfR6Qz8q1XAHjL-GjUhvzToJd7bU09FO9ctMkG0wxM5VuFeeFLLjtVK8ZnkpNkuGo-w6CTTJ9Z3PwsBAemlbUF934W8iy5DpaZtOUcU02-ZLcaS51jHEkTFm_kY1_wfOO8QnXrb8hBzDEc6pgZ4gFoyz4KgiD7nxfTe8ghqAhIfrJ46cTzVZBbkPlODVJsLCDO6V7ZcJoncyw1yRr0hd1GNn_ZbEM3I9i1bmVxOlWElUvDUNHxpQngt3C4CXzjS1rtvkw22wMrTRtTbC8Lkuabe7jvthPPe3DofYCAAA=)): @@ -34,6 +52,32 @@ This is particularly useful when `Parent.svelte` is not directly aware of `Child ``` +## `setContext` and `getContext` + +As an alternative to `createContext`, you can use `setContext` and `getContext` directly. The parent component sets context with `setContext(key, value)`... + +```svelte + + +``` + +...and the child retrieves it with `getContext`: + +```svelte + + + +

{message}, inside Child.svelte

+``` + The key (`'my-context'`, in the example above) and the context itself can be any JavaScript value. In addition to [`setContext`](svelte#setContext) and [`getContext`](svelte#getContext), Svelte exposes [`hasContext`](svelte#hasContext) and [`getAllContexts`](svelte#getAllContexts) functions. @@ -44,14 +88,14 @@ You can store reactive state in context ([demo](/playground/untitled#H4sIAAAAAAA ```svelte - - ``` -...though note that if you _reassign_ `counter` instead of updating it, you will 'break the link' — in other words instead of this... +...though note that if you _reassign_ `user` instead of updating it, you will 'break the link' — in other words instead of this... ```svelte - ``` @@ -118,7 +116,7 @@ You can store reactive state in context ([demo](/playground/untitled#H4sIAAAAAAA ...you must do this: ```svelte - ```