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)
}
jetli commented
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
ctron commented
Yea, I thought about that too. But doesn't that re-render every time?