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
The new leptos 0.7 release notes recommends using channels to send views rather than storing views in signals which was previously possible in 0.6. However, I'm running into problem where the context is unavailable to views sent through a channel. Here is a minimal example of what I'm trying to accomplish:
use leptos::prelude::*;
#[derive(Clone)]
struct MyContext(String);
#[component]
fn CanAccessContext() -> impl IntoView {
let context = use_context::<MyContext>().expect("MyContext to be accessable");
view! {
<div>{format!("Initial run - Context message: {:?}", context.0)}</div>
}
}
#[component]
fn CannotAccessContext() -> impl IntoView {
let context = use_context::<MyContext>().expect("MyContext to be accessable");
view! {
<div>{format!("Subsequent - Context message: {:?}", context.0)}</div>
}
}
#[component]
fn SendsMessage(
tx: std::sync::mpsc::Sender<AnyView>,
trigger: ArcTrigger
) -> impl IntoView {
let _ = tx.send(
view! {
<CanAccessContext/>
}.into_any()
);
trigger.notify();
let second_message = move |_| {
let _ = tx.send(
view! {
<CannotAccessContext/>
}.into_any()
);
trigger.notify();
};
view! {
<div on:click=second_message>
This element sends a view - click to send another one
</div>
}
}
#[component]
fn App() -> impl IntoView {
provide_context(MyContext(String::from("Hello from context!")));
let trigger = ArcTrigger::new();
let (tx, rx) = std::sync::mpsc::channel();
view! {
<div>
<SendsMessage tx={tx.clone()} trigger={trigger.clone()}/>
{move || {
trigger.track();
rx.try_recv().unwrap_or_else(|_| view! {
<div>Nothing received</div>
}.into_any())
}}
</div>
}
}
fn main() {
console_error_panic_hook::set_once();
mount_to_body(|| {
view! {
<App/>
}
})
}
The first view that is received on the channel is able to access the context, however any subsequent views received are unable to access the context.
I've considered the alternative of manually sending props to any element sent through a channel, however my application depends a lot on the context api, this would result in significant restructuring...
Am I missing something obvious? Any help would be greatly appreciated!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The new leptos 0.7 release notes recommends using channels to send views rather than storing views in signals which was previously possible in 0.6. However, I'm running into problem where the context is unavailable to views sent through a channel. Here is a minimal example of what I'm trying to accomplish:
The first view that is received on the channel is able to access the context, however any subsequent views received are unable to access the context.
I've considered the alternative of manually sending props to any element sent through a channel, however my application depends a lot on the context api, this would result in significant restructuring...
Am I missing something obvious? Any help would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions