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:36:05 +08:00
1 parent 24ebc1e628
commit 5a73147d69
3 files changed
+10 -7

No files matched your search

+5 -2
View File
@@ -45,8 +45,11 @@ pub fn use_context<T: 'static + Clone>() -> T {
// This component does read from the signal, so when the signal changes it will rerun // This component does read from the signal, so when the signal changes it will rerun
///#[component] ///#[component]
///fn Child() -> Element { ///fn Child() -> Element {
/// let signal: Signal<i32> = use_context(); /// let signal: Signal<i32> = use_context();
/// rsx! { "{signal}" } /// rsx! {
/// button { onclick: move |_| signal += 1, "increment context" }
/// p {"{signal}"}
/// }
///} ///}
/// ``` /// ```
pub fn use_context_provider<T: 'static + Clone>(f: impl FnOnce() -> T) -> T { pub fn use_context_provider<T: 'static + Clone>(f: impl FnOnce() -> T) -> T {
+1 -1
View File
@@ -1,7 +1,7 @@
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use dioxus_signals::ReactiveContext; use dioxus_signals::ReactiveContext;
/// use_effect will subscribe to any changes in the signal values it captures /// `use_effect` will subscribe to any changes in the signal values it captures
/// effects will always run after first mount and then whenever the signal values change /// effects will always run after first mount and then whenever the signal values change
/// If the use_effect call was skipped due to an early return, the effect will no longer activate. /// If the use_effect call was skipped due to an early return, the effect will no longer activate.
/// ```rust /// ```rust
+4 -4
View File
@@ -8,17 +8,17 @@ use dioxus_signals::*;
use dioxus_signals::{Readable, Writable}; use dioxus_signals::{Readable, Writable};
use std::future::Future; use std::future::Future;
/// A hook that allows you to spawn a future /// A hook that allows you to spawn a future.
/// /// This future will **not** run on the server
/// The future is spawned on the next call to `flush_sync` which means that it will not run on the server. /// 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. /// 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**. /// `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. /// If you want to return a value, use `use_resource` instead.
/// ```rust /// ```rust
/// fn app() -> Element { /// fn app() -> Element {
/// let mut count = use_signal(|| 0); /// let mut count = use_signal(|| 0);
/// let mut running = use_signal(|| true); /// let mut running = use_signal(|| true);
/// // use_future will spawn an infinitely running future that can be started and stopped /// // `use_future` will spawn an infinitely running future that can be started and stopped
/// use_future(move || async move { /// use_future(move || async move {
/// loop { /// loop {
/// if running() { /// if running() {