docs: clarify context state getter pattern (#18226)

Closes #18022

This updates the context docs to show the getter pattern for state that
may be reassigned, such as primitive state. The new example passes a
function through context so children read the current value instead of
capturing the initial value.

This is a docs-only clarification and does not change runtime behavior.

Validation:

- `git diff --check`
- `pnpm exec prettier --check
documentation/docs/06-runtime/02-context.md`

Full `pnpm test`/`pnpm lint` were not run because this only changes
documentation.

---------

Co-authored-by: Rich Harris <hello@rich-harris.dev>
pull/18241/head
Mukunda Rao Katta 4 days ago committed by GitHub
parent a5df6616ea
commit a63bf862c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -163,6 +163,8 @@ export const [getCounter, setCounter] = createContext<Counter>();
Svelte will warn you if you get it wrong.
Similarly, to pass primitive values through context, use functions as described in [Passing state into functions]($state#Passing-state-into-functions).
## Component testing
When writing [component tests](testing#Unit-and-component-tests-with-Vitest-Component-testing), it can be useful to create a wrapper component that sets the context in order to check the behaviour of a component that uses it. As of version 5.49, you can do this sort of thing:

Loading…
Cancel
Save