2022-07-07 08:50:36 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(App);
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Clone)]
|
|
|
|
struct Comment {
|
|
|
|
content: String,
|
|
|
|
id: usize,
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
#[rustfmt::skip]
|
2022-07-07 08:50:36 +00:00
|
|
|
pub fn App(cx: Scope) -> Element {
|
|
|
|
// ANCHOR: render_list
|
2023-01-06 23:00:12 +00:00
|
|
|
let comment_field = use_state(cx, String::new);
|
|
|
|
let mut next_id = use_state(cx, || 0);
|
|
|
|
let comments = use_ref(cx, Vec::<Comment>::new);
|
2022-07-07 08:50:36 +00:00
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
let comments_lock = comments.read();
|
|
|
|
let comments_rendered = comments_lock.iter().map(|comment| {
|
|
|
|
rsx!(CommentComponent {
|
|
|
|
key: "{comment.id}",
|
|
|
|
comment: comment.clone(),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
form {
|
|
|
|
onsubmit: move |_| {
|
|
|
|
comments.write().push(Comment {
|
|
|
|
content: comment_field.get().clone(),
|
|
|
|
id: *next_id.get(),
|
|
|
|
});
|
|
|
|
next_id += 1;
|
|
|
|
|
|
|
|
comment_field.set(String::new());
|
2022-07-07 08:50:36 +00:00
|
|
|
},
|
2023-01-06 23:00:12 +00:00
|
|
|
input {
|
|
|
|
value: "{comment_field}",
|
|
|
|
oninput: |event| comment_field.set(event.value.clone()),
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
r#type: "submit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
comments_rendered,
|
|
|
|
))
|
2022-07-07 08:50:36 +00:00
|
|
|
// ANCHOR_END: render_list
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
pub fn AppForLoop(cx: Scope) -> Element {
|
|
|
|
// ANCHOR: render_list_for_loop
|
|
|
|
let comment_field = use_state(cx, String::new);
|
|
|
|
let mut next_id = use_state(cx, || 0);
|
|
|
|
let comments = use_ref(cx, Vec::<Comment>::new);
|
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
form {
|
|
|
|
onsubmit: move |_| {
|
|
|
|
comments.write().push(Comment {
|
|
|
|
content: comment_field.get().clone(),
|
|
|
|
id: *next_id.get(),
|
|
|
|
});
|
|
|
|
next_id += 1;
|
|
|
|
|
|
|
|
comment_field.set(String::new());
|
|
|
|
},
|
|
|
|
input {
|
|
|
|
value: "{comment_field}",
|
|
|
|
oninput: |event| comment_field.set(event.value.clone()),
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
r#type: "submit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
for comment in &*comments.read() {
|
|
|
|
// Notice the body of this for loop is rsx code, not an expression
|
|
|
|
CommentComponent {
|
|
|
|
key: "{comment.id}",
|
|
|
|
comment: comment.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
// ANCHOR_END: render_list_for_loop
|
|
|
|
}
|
|
|
|
|
2022-07-07 08:50:36 +00:00
|
|
|
#[inline_props]
|
|
|
|
fn CommentComponent(cx: Scope, comment: Comment) -> Element {
|
|
|
|
cx.render(rsx!(div {
|
|
|
|
"Comment by anon:",
|
|
|
|
p { "{comment.content}" }
|
|
|
|
button { "Reply" },
|
|
|
|
}))
|
|
|
|
}
|