docs: improve createContext documentation (#16952)

* docs: improve createContext documentation

* update context page

* bump
pull/16243/merge
Rich Harris 2 days ago committed by GitHub
parent ec99d7bc4c
commit d6d13ced08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -83,27 +83,18 @@ Svelte will warn you if you get it wrong.
## Type-safe context ## Type-safe context
A useful pattern is to wrap the calls to `setContext` and `getContext` inside helper functions that let you preserve type safety: As an alternative to using `setContext` and `getContext` directly, you can use them via `createContext`. This gives you type safety and makes it unnecessary to use a key:
```js ```ts
/// file: context.js /// file: context.ts
// @filename: ambient.d.ts // @filename: ambient.d.ts
interface User {} interface User {}
// @filename: index.js // @filename: index.ts
// ---cut--- // ---cut---
import { getContext, setContext } from 'svelte'; import { createContext } from 'svelte';
const key = {};
/** @param {User} user */ export const [getUserContext, setUserContext] = createContext<User>();
export function setUserContext(user) {
setContext(key, user);
}
export function getUserContext() {
return /** @type {User} */ (getContext(key));
}
``` ```
## Replacing global state ## Replacing global state

@ -71,8 +71,12 @@ export function set_dev_current_component_function(fn) {
/** /**
* Returns a `[get, set]` pair of functions for working with context in a type-safe way. * Returns a `[get, set]` pair of functions for working with context in a type-safe way.
*
* `get` will throw an error if no parent component called `set`.
*
* @template T * @template T
* @returns {[() => T, (context: T) => T]} * @returns {[() => T, (context: T) => T]}
* @since 5.40.0
*/ */
export function createContext() { export function createContext() {
const key = {}; const key = {};
@ -93,6 +97,8 @@ export function createContext() {
* Retrieves the context that belongs to the closest parent component with the specified `key`. * Retrieves the context that belongs to the closest parent component with the specified `key`.
* Must be called during component initialisation. * Must be called during component initialisation.
* *
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
*
* @template T * @template T
* @param {any} key * @param {any} key
* @returns {T} * @returns {T}
@ -110,6 +116,8 @@ export function getContext(key) {
* *
* Like lifecycle functions, this must be called during component initialisation. * Like lifecycle functions, this must be called during component initialisation.
* *
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
*
* @template T * @template T
* @param {any} key * @param {any} key
* @param {T} context * @param {T} context

@ -13,6 +13,7 @@ export function set_ssr_context(v) {
/** /**
* @template T * @template T
* @returns {[() => T, (context: T) => T]} * @returns {[() => T, (context: T) => T]}
* @since 5.40.0
*/ */
export function createContext() { export function createContext() {
const key = {}; const key = {};

@ -450,12 +450,18 @@ declare module 'svelte' {
type NotFunction<T> = T extends Function ? never : T; type NotFunction<T> = T extends Function ? never : T;
/** /**
* Returns a `[get, set]` pair of functions for working with context in a type-safe way. * Returns a `[get, set]` pair of functions for working with context in a type-safe way.
* */ *
* `get` will throw an error if no parent component called `set`.
*
* @since 5.40.0
*/
export function createContext<T>(): [() => T, (context: T) => T]; export function createContext<T>(): [() => T, (context: T) => T];
/** /**
* Retrieves the context that belongs to the closest parent component with the specified `key`. * Retrieves the context that belongs to the closest parent component with the specified `key`.
* Must be called during component initialisation. * Must be called during component initialisation.
* *
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
*
* */ * */
export function getContext<T>(key: any): T; export function getContext<T>(key: any): T;
/** /**
@ -465,6 +471,8 @@ declare module 'svelte' {
* *
* Like lifecycle functions, this must be called during component initialisation. * Like lifecycle functions, this must be called during component initialisation.
* *
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
*
* */ * */
export function setContext<T>(key: any, context: T): T; export function setContext<T>(key: any, context: T): T;
/** /**

Loading…
Cancel
Save