This is outdated documentation for Sycamore.
For up-to-date documentation, see the latest version (v0.8).
Contexts
Contexts provide an easy way to share data between components without drilling props through multiple levels of the component hierarchy.
Creating a ContextProvider
is required before any components can use the context. The value used
should implement Clone
.
Using ContextProvider
ContextProvider
is a component like any other. It takes a value
prop which is the context value
and a children
prop which is the child components that have access to the context value.
Using use_context
use_context
returns a clone of the value for a context of a given type.
Example
use *;
use ;
;
template!
Remember that unlike contexts in React and many other libraries, the value
prop is not reactive by
itself. This is because components only run once. In order to make a context value reactive, you
need to use a Signal
or other reactive data structure.