jetli/yew-hooks

use_async with a parameter from state

ctron opened this issue · 2 comments

ctron commented

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 commented

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