Function freya::prelude::use_resource

pub fn use_resource<T, F>(future: impl FnMut() -> F + 'static) -> Resource<T>
where T: 'static, F: Future<Output = T> + 'static,
Expand description

A memo that resolves to a value asynchronously. Similar to use_future but use_resource returns a value. See Resource for more details.

fn app() -> Element {
    let country = use_signal(|| WeatherLocation {
        city: "Berlin".to_string(),
        country: "Germany".to_string(),
        coordinates: (52.5244, 13.4105)
    });

   // Because the resource's future subscribes to `country` by reading it (`country.read()`),
   // every time `country` changes the resource's future will run again and thus provide a new value.
   let current_weather = use_resource(move || async move { get_weather(&country()).await });
   
   rsx! {
       // the value of the resource can be polled to
       // conditionally render elements based off if it's future
       // finished (Some(Ok(_)), errored Some(Err(_)),
       // or is still running (None)
       match current_weather.value() {
           Some(Ok(weather)) => rsx! { WeatherElement { weather } },
           Some(Err(e)) => rsx! { p { "Loading weather failed, {e}" } },
           None =>  rsx! { p { "Loading..." } }
       }
   }
}

§With non-reactive dependencies

To add non-reactive dependencies, you can use the use_reactive hook.

Signals will automatically be added as dependencies, so you don’t need to call this method for them.


#[component]
fn Comp(count: u32) -> Element {
    // Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
    let new_count = use_resource(use_reactive((&count, |(count,)| async move {count + 1} )));

    todo!()
}