Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_context Error after updating to 0.9.0-beta.2 #640

Closed
jhartma opened this issue Oct 16, 2023 · 4 comments
Closed

use_context Error after updating to 0.9.0-beta.2 #640

jhartma opened this issue Oct 16, 2023 · 4 comments
Labels
A-async Area: futures, suspense, and async/await A-reactivity Area: reactivity and state handling C-bug Category: bug, something isn't working

Comments

@jhartma
Copy link

jhartma commented Oct 16, 2023

Describe the bug
After updating my working app to 0.9.0-beta.2, I get the following runtime error message in the console

sycamore-reactive-0.9.0-beta.2/src/context.rs:45:34: invalid SlotMap key used

This is what causes the error in my code:

// main component
fn main() {
    sycamore::render(|| {
        console_error_panic_hook::set_once();

        // Create a default empty app state
        let state = AppState::new();

        // Render app
        view! {
            app::App(state=state)
        }
    });
}

// Router component
#[component]
pub async fn App<G: Html>(props: AppProps) -> View<G> {
    ...
    provide_context(props.state);

    view!{ ... }
}

// This component produces the error
#[component]
pub async fn Login<G: Html>() -> View<G> {
    ...

    let login: Rc<Box<dyn Fn(String, String) -> ()>> =
        Rc::new(Box::new(move |email: String, password: String| {
            ...

            spawn_local_scoped(async move {
               ...
               let state = use_context::<AppState>();   // <-- results in error
            })
       }))

}

Environment

  • Sycamore: 0.9.0-beta.2
  • Browser: chrome
  • OS: Linux
@jhartma
Copy link
Author

jhartma commented Oct 16, 2023

The problem seems to be the Rc wrapper, once I move use_context out of it, it works

@lukechu10
Copy link
Collaborator

Ah this might be something to do with the interaction between async components and reactive context. The error message seems to point out that there is no current_node set in Root when we are inside an async scope. I'll have to look more into this.

@lukechu10 lukechu10 added C-bug Category: bug, something isn't working A-reactivity Area: reactivity and state handling A-async Area: futures, suspense, and async/await labels Oct 16, 2023
@lukechu10
Copy link
Collaborator

Oh I just realised are you calling login in an event handler? If that's the case, this would be expected behaviour. Event handlers are called at the root scope where the context is not defined, thereby causing the error. The way to solve this would be to hoist the use_context out of the event handler.

@jhartma
Copy link
Author

jhartma commented Oct 21, 2023

Thanks for clarifying this!

In the 0.8.2 version, I used a global state struct that had many methods that needed a handle on the state struct itself, and since context was available everywhere, I pulled in the state via use_context. But since actions where called by an event handler, I got this error all over the place when updating to 0.9.0-beta.2. Using use_context only in components and passing the state around solved it finally.

@jhartma jhartma closed this as completed Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async Area: futures, suspense, and async/await A-reactivity Area: reactivity and state handling C-bug Category: bug, something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants