diff --git a/docs/guide/src/en/async/use_effect.md b/docs/guide/src/en/async/use_effect.md index 4980e2d20..bd73453a9 100644 --- a/docs/guide/src/en/async/use_effect.md +++ b/docs/guide/src/en/async/use_effect.md @@ -4,7 +4,6 @@ Whenever the hooks [dependencies](#dependencies) change, the future will be re-evaluated. This is useful to syncrhonize with external events. - ## Dependencies You can make the future re-run when some value changes. For example, you might want to fetch a user's data only when the user id changes. You can provide a tuple of "dependencies" to the hook. It will automatically re-run the future when any of those dependencies change. @@ -13,23 +12,26 @@ Example: ```rust, no_run #[inline_props] -fn Profile(cx: Scope, id: &str) -> Element { - let name = use_state(cx, || "Default name"); +fn Profile(cx: Scope, id: usize) -> Element { + let name = use_state(cx, || None); // Only fetch the user data when the id changes. - use_effect(cx, (id,), |(id,)| async move { - let user = fetch_user(id).await; - name.set(user.name); + use_effect(cx, (id,), |(id,)| { + to_owned![name]; + async move { + let user = fetch_user(id).await; + name.set(user.name); + } }); + let name = name.get().clone().unwrap_or("Loading...".to_string()); + render!( p { "{name}" } ) } fn app(cx: Scope) -> Element { - render!( - Profile { id: "dioxusLabs" } - ) + render!(Profile { id: 0 }) } -``` \ No newline at end of file +``` diff --git a/docs/guide/src/en/interactivity/memoization.md b/docs/guide/src/en/interactivity/memoization.md index cc67c02c4..72a709bcc 100644 --- a/docs/guide/src/en/interactivity/memoization.md +++ b/docs/guide/src/en/interactivity/memoization.md @@ -8,16 +8,12 @@ fn Calculator(cx: Scope, number: usize) -> Element { let bigger_number = use_memo(cx, (number,), |(number,)| { // This will only be calculated when `number` has changed. number * 100 - }): - + }); render!( p { "{bigger_number}" } ) } - fn app(cx: Scope) -> Element { - render!( - Calculator { number: 0 } - ) + render!(Calculator { number: 0 }) } ``` diff --git a/packages/hooks/src/useeffect.rs b/packages/hooks/src/useeffect.rs index b52b89c3c..aec1f9c9a 100644 --- a/packages/hooks/src/useeffect.rs +++ b/packages/hooks/src/useeffect.rs @@ -14,25 +14,28 @@ use crate::UseFutureDep; /// ## Examples /// /// ```rust, no_run -/// /// #[inline_props] -/// fn Profile(cx: Scope, id: &str) -> Element { -/// let name = use_state(cx, || "Default name"); +/// fn Profile(cx: Scope, id: usize) -> Element { +/// let name = use_state(cx, || None); /// -/// use_effect(cx, (id,), |(id,)| async move { -/// let user = fetch_user(id).await; -/// name.set(user.name); +/// // Only fetch the user data when the id changes. +/// use_effect(cx, (id,), |(id,)| { +/// to_owned![name]; +/// async move { +/// let user = fetch_user(id).await; +/// name.set(user.name); +/// } /// }); /// +/// let name = name.get().clone().unwrap_or("Loading...".to_string()); +/// /// render!( /// p { "{name}" } /// ) /// } /// /// fn app(cx: Scope) -> Element { -/// render!( -/// Profile { id: "dioxusLabs" } -/// ) +/// render!(Profile { id: 0 }) /// } /// ``` pub fn use_effect(cx: &ScopeState, dependencies: D, future: impl FnOnce(D::Out) -> F) diff --git a/packages/hooks/src/usememo.rs b/packages/hooks/src/usememo.rs index f99d1e893..d3d991ccc 100644 --- a/packages/hooks/src/usememo.rs +++ b/packages/hooks/src/usememo.rs @@ -16,17 +16,13 @@ use crate::UseFutureDep; /// let bigger_number = use_memo(cx, (number,), |(number,)| { /// // This will only be calculated when `number` has changed. /// number * 100 -/// }): -/// +/// }); /// render!( /// p { "{bigger_number}" } /// ) /// } -/// /// fn app(cx: Scope) -> Element { -/// render!( -/// Calculator { number: 0 } -/// ) +/// render!(Calculator { number: 0 }) /// } /// ``` pub fn use_memo(cx: &ScopeState, dependencies: D, callback: impl FnOnce(D::Out) -> T) -> &T