yew-hooks icon indicating copy to clipboard operation
yew-hooks copied to clipboard

use_async with a parameter from state

Open ctron opened this issue 2 years ago • 2 comments

Assuming a use-case of:

  • Enter a parameter value
  • Issue the request (with that parameter)

It looks like with the current use_async variant, the future already gets created with every re-render, but not being used until the .run method is somehow invoked.

I think it would be nice to have some kind of "dependency" to the hook, which allows to provide one or more dependencies, parameters, which are being used to generate the future.

Something in the direction of:

#[function_component]
fn component() -> Html {
  let state = use_state_eq(String::new);

  let call = use_async(|input|{
    async move {
      fetch(input); // input = value of state
    }
  }, state.clone());

  let onclick = {
    let call = call.clone();
    use_callack(move |()|{
      call.run();
    })
  };

  html!() // somehow updates `state` (e.g. through an input field)
}

ctron avatar Feb 16 '23 08:02 ctron

for now , you can just move your dependency states to use_async, see here: https://github.com/jetli/rust-yew-realworld-example-app/blob/3a767a98600ee89e8b79fb23dcd9a7539ed59f36/crates/conduit-wasm/src/routes/login.rs#L20

jetli avatar Feb 16 '23 08:02 jetli

Yea, I thought about that too. But doesn't that re-render every time?

ctron avatar Feb 17 '23 07:02 ctron