You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
According to https://docs.rs/leptos/latest/leptos/fn.use_context.html, a context that is provided higher in the component tree can be used lower down, but a context that is provided lower in the tree cannot be used higher up. unfortunately, something is broken!
Leptos Dependencies
leptos = { version = "0.5.2", features = ["csr", "nightly"] }
console_error_panic_hook = "0.1.7"
To Reproduce
use leptos::*;fnmain(){
console_error_panic_hook::set_once();mount_to_body(|| view!{<App/>})}#[component]fnApp() -> implIntoView{provide_context("parent_context");view!{
<Child /> // this is receiving "parent_context" as expected
<Child /> // but this is receiving "child_context" instead of "parent_context"!}}#[component]fnChild() -> implIntoView{let context = expect_context::<&'static str>();// first, we receive context from parent (just before the override)provide_context("child_context");// then we provide context under the same typeview!{
<div>{format!("child (context: {context})")}</div>
}}
Expected behavior
The second child should receive "parent_context" instead of "child_context"
Screenshots
Additional context
None
The text was updated successfully, but these errors were encountered:
Thanks. This behavior changed in 0.5. Pre-0.5, a new component created a new Scope. 0.5 and later, this is not the case and so context simply follows the reactive graph; components do not "exist" at runtime other than as setup functions. In your example, because there is no reactivity, all the contexts are provided in the root scope.
Making these exist in separate reactive owners (by wrapping in a closure to create an effect) works as expected:
This is somewhat awkward syntax. Luckily, a component with no props is just a zero-argument function, and so that's actually the same as doing this, which also works as expected
Thinking about it more this is probably one of the two reasons Solid uses a context provider component instead (the other being the need for unique ID symbols, which we handle with types instead)
Obviously I need to fix the docs, as they are now wrong.
Describe the bug
According to https://docs.rs/leptos/latest/leptos/fn.use_context.html, a context that is provided higher in the component tree can be used lower down, but a context that is provided lower in the tree cannot be used higher up. unfortunately, something is broken!
Leptos Dependencies
To Reproduce
Expected behavior
The second child should receive "parent_context" instead of "child_context"
Screenshots
Additional context
None
The text was updated successfully, but these errors were encountered: