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

+24
View File
@@ -12,6 +12,30 @@ use std::future::Future;
///
/// The future is spawned on the next call to `flush_sync` which means that it will not run on the server.
/// To run a future on the server, you should use `spawn` directly.
/// use_future assumes your future will never complete - **it won't return a value**.
/// If you want to return a value, use `use_resource` instead.
/// ```rust
/// fn app() -> Element {
/// let mut count = use_signal(|| 0);
/// let mut running = use_signal(|| true);
/// // use_future will spawn an infinitely running future that can be started and stopped
/// use_future(move || async move {
/// loop {
/// if running() {
/// count += 1;
/// }
/// tokio::time::sleep(Duration::from_millis(400)).await;
/// }
/// });
/// rsx! {
/// div {
/// h1 { "Current count: {count}" }
/// button { onclick: move |_| running.toggle(), "Start/Stop the count"}
/// button { onclick: move |_| count.set(0), "Reset the count" }
/// }
/// }
/// }
/// ```
pub fn use_future<F>(mut future: impl FnMut() -> F + 'static) -> UseFuture
where
F: Future + 'static,