-
after switching to version 0.5 for example, in Loaded a context is created (provide_context) in the create_frame_context function if you remove #[component]
#[allow(non_snake_case)]
pub fn GameInit(text: Option<String>) -> impl IntoView {
let data = from_json_string::<GameData>(text).expect("source data parse error");
let images = load_image(&data.src_list[0]);
let fps = use_fps();
view! {
<Show when=move || (images.get().is_some()) fallback=|| view! { <></> }>
<Loaded data=data.clone() image=images.get_untracked().unwrap() fps/>
</Show>
}
}
#[component]
#[allow(non_snake_case)]
fn Loaded(data: GameData, image: HtmlImageElement, fps: ReadSignal<Option<Fps>>) -> impl IntoView {
create_frame_context(Some(InitBoardSize {
width: image.natural_width(),
height: image.natural_height(),
..InitBoardSize::default()
}));
view! {
<HostGame>
<BoardView data image browser_fps=fps/>
</HostGame>
}
} Replacing move|| if images.get().is_some(){
div().child(Loaded(LoadedProps{data: data.clone(), image:images.get_untracked().unwrap(), fps }))
}else {
div()
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I can't see the #[component]
fn SomeComponent() -> impl IntoView {
view! {
<button on:click=|_| {
let ctx = use_context::<T>();
ctx.something();
}> // etc.
}
} you should do #[component]
fn SomeComponent() -> impl IntoView {
// access it in component body
let ctx = use_context::<T>();
view! {
// move it into click handler
<button on:click=move |_| {
ctx.something();
}> // etc.
}
} If that is impossible -- i.e., if the context is provided at some point other than during the initial render -- you can manually reset to the component's owner in the handler. #[component]
fn SomeComponent() -> impl IntoView {
let owner = Owner::current().unwrap();
view! {
<button on:click=|_| {
let ctx = with_owner(owner, || use_context::<T>());
ctx.something();
}> // etc.
}
} I have a short bullet point on this in the release notes, but will add a link to this longer version! |
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick response I have made several browser games to solve the problem described above and everything worked |
Beta Was this translation helpful? Give feedback.
-
this is how I work now (v0.5) with use_context inside events #[component]
fn SomeComponent() -> impl IntoView {
let on_click = move |_: MouseEvent| {
let ctx = use_context::<T>();
ctx.something();
};
view! {
<button on:click=on_click />
}
} thanks for the example with |
Beta Was this translation helpful? Give feedback.
I can't see the
use_context
in the example, but you mention events, so I assume you mean callinguse_context
inside an event listener returnsNone
. This is true and is a breaking change in 0.5; you can only access context in component bodies. So instead ofyou should do
If that is impossible…