forked from dioxus-community/dioxus-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.rs
65 lines (55 loc) · 1.52 KB
/
demo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use dioxus::prelude::*;
use dioxus_radio::prelude::*;
#[derive(Default)]
struct Data {
pub lists: Vec<Vec<String>>,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum DataChannel {
ListCreated,
ListN(usize),
}
impl RadioChannel<Data> for DataChannel {}
fn main() {
dioxus::launch(|| {
use_init_radio_station::<Data, DataChannel>(Data::default);
let mut radio = use_radio::<Data, DataChannel>(DataChannel::ListCreated);
let onclick = move |_| {
radio.write().lists.push(Vec::default());
};
println!("Running DataChannel::ListCreated");
rsx!(
button {
onclick,
"Add new list",
}
for (list_n, _) in radio.read().lists.iter().enumerate() {
ListComp {
list_n
}
}
)
});
}
#[allow(non_snake_case)]
#[component]
fn ListComp(list_n: usize) -> Element {
let mut radio = use_radio::<Data, DataChannel>(DataChannel::ListN(list_n));
println!("Running DataChannel::ListCreated({list_n})");
rsx!(
div {
button {
onclick: move |_| radio.write().lists[list_n].push("Hello World".to_string()),
"New Item"
},
ul {
for (i, item) in radio.read().lists[list_n].iter().enumerate() {
li {
key: "{i}",
"{item}"
}
}
}
}
)
}