Skip to content

create_resource get and and_then won't work inside event handler? #2252

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

You must be logged in to vote

This is not how .and_then() works — it is a way of reactively accessing the value of a resource, so it needs to be used with an effect, just like accessing a signal would.

Your code should either

  1. not use a resource, but just use spawn_local to do some async work in the file_handle handler (this is what I would recommend)
let file_handle = move |e: Event| {
        set_spinner(true);
        let input: HtmlInputElement = event_target(&e);
        let file_data = input.files().unwrap().get(0);
        gloo::console::log!(format! {"{:?}", file_data});
        spawn_local(async move {
            let data = get_cid_ipfs(file_data).await;
            // etc.
        });
    };
  1. use the resou…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@amiyatulu
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by amiyatulu
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
3 participants