Skip to content

0.5 version - context in <Show /> #1857

Answered by gbj
azecm asked this question in Q&A
Discussion options

You must be logged in to vote

I can't see the use_context in the example, but you mention events, so I assume you mean calling use_context inside an event listener returns None. This is true and is a breaking change in 0.5; you can only access context in component bodies. So instead of

#[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…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by azecm
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants