Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nonreactive examples for hooks were not compilable as written #2337

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/router_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Example: Updating components with use_resource
//! -----------------
//!
//! This example shows how to use use_reactive to update a component properly
//! when linking to it from the same component, when using use_resource

use dioxus::prelude::*;

#[derive(Clone, Routable, Debug, PartialEq)]
enum Route {
#[route("/")]
Home {},
#[route("/blog/:id")]
Blog { id: i32 },
}

fn main() {
launch(App);
}

#[component]
fn App() -> Element {
rsx! {
Router::<Route> {}
}
}

#[component]
fn Blog(id: ReadOnlySignal<i32>) -> Element {
async fn future(n: i32) -> i32 {
n
}

// if you use the naive approach, the "Blog post {id}" below will never update when clicking links!
// let res = use_resource(move || future(id));

// the use_reactive hook is required to properly update when clicking links to this component, from this component
ealmloff marked this conversation as resolved.
Show resolved Hide resolved
let res = use_resource(move || future(id()));

match res() {
ealmloff marked this conversation as resolved.
Show resolved Hide resolved
Some(id) => rsx! {
div {
"Blog post {id}"
}
for i in 0..10 {
div {
Link { to: Route::Blog { id: i }, "Go to Blog {i}" }
}
}
},
None => rsx! {},
}
}

#[component]
fn Home() -> Element {
rsx! {
Link {
to: Route::Blog {
id: 0
},
"Go to blog"
}
}
}
Loading