added better documentation and code snipppets for the use_resource, use_future, use_effect and use_context hooks

This commit is contained in:
andrey committed 2024-02-27 10:15:38 +08:00
1 parent 451a8f231e
commit df0d45be48
8 files changed
+99 -7

No files matched your search

+25 -1
View File
@@ -10,8 +10,32 @@ use futures_util::{future, pin_mut, FutureExt};
use std::future::Future;
/// A memo that resolve to a value asynchronously.
/// Unlike `use_future`, `use_resource` runs on the **server**
/// See [`Resource`] for more details.
/// ```rust
///fn app() -> Element {
/// let country = use_signal(|| WeatherLocation {
/// city: "Berlin".to_string(),
/// country: "Germany".to_string(),
/// coordinates: (52.5244, 13.4105)
/// });
///
/// This runs on the server
/// let current_weather = //run a future inside the use_resource hook
/// use_resource(move || async move { get_weather(&country.read().clone()).await });
///
/// rsx! {
/// //the value of the future can be polled to
/// //conditionally render elements based off if the future
/// //finished (Some(Ok(_)), errored Some(Err(_)),
/// //or is still finishing (None)
/// match current_weather.value() {
/// Some(Ok(weather)) => WeatherElement { weather },
/// Some(Err(e)) => p { "Loading weather failed, {e}" }
/// None => p { "Loading..." }
/// }
/// }
///}
/// ```
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
pub fn use_resource<T, F>(future: impl Fn() -> F + 'static) -> Resource<T>
where