Skip to content

Commit dfa8d5c

Browse files
committed
remove context
1 parent f58f141 commit dfa8d5c

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

frameworks/keyed/dioxus/src/main.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ fn main() {
1414
#[derive(Clone, PartialEq)]
1515
struct Label {
1616
key: usize,
17-
label: Tracked<String>,
17+
label: Signal<String>,
1818
}
1919

2020
impl Label {
21-
fn new(cx: &ScopeState, num: usize, label: String) -> Self {
21+
fn new( num: usize, label: String) -> Self {
2222
Label {
2323
key: num,
24-
label: Tracked::new(cx, label),
24+
label: use_signal(|| label),
2525
}
2626
}
2727

28-
fn new_list(cx: &ScopeState, num: usize, key_from: usize) -> Vec<Self> {
28+
fn new_list( num: usize, key_from: usize) -> Vec<Self> {
2929
let mut labels = Vec::with_capacity(num);
30-
append(cx, &mut labels, num, key_from);
30+
append(&mut labels, num, key_from);
3131
labels
3232
}
3333
}
3434

35-
fn append(cx: &ScopeState,list: &mut Vec<Label>, num: usize, key_from: usize) {
35+
fn append( list: &mut Vec<Label>, num: usize, key_from: usize) {
3636
list.reserve_exact(num);
3737
for x in 0..num {
3838
let adjective = ADJECTIVES[random(ADJECTIVES.len())];
@@ -44,7 +44,7 @@ fn append(cx: &ScopeState,list: &mut Vec<Label>, num: usize, key_from: usize) {
4444
label.push_str(colour);
4545
label.push(' ');
4646
label.push_str(noun);
47-
list.push(Label::new(cx, x + key_from, label));
47+
list.push(Label::new(x + key_from, label));
4848
}
4949
}
5050

@@ -55,23 +55,23 @@ struct LabelsContainer {
5555
}
5656

5757
impl LabelsContainer {
58-
fn new(cx: &ScopeState,num: usize, last_key: usize) -> LabelsContainer {
59-
let labels = Label::new_list(cx, num, last_key + 1);
58+
fn new(num: usize, last_key: usize) -> LabelsContainer {
59+
let labels = Label::new_list(num, last_key + 1);
6060
LabelsContainer {
6161
labels,
6262
last_key: last_key + num,
6363
}
6464
}
6565

66-
fn append(&mut self, cx: &ScopeState,num: usize) {
66+
fn append(&mut self, num: usize) {
6767
self.labels.reserve(num);
68-
append(cx, &mut self.labels, num, self.last_key + 1);
68+
append(&mut self.labels, num, self.last_key + 1);
6969
self.last_key += num;
7070
}
7171

72-
fn overwrite(&mut self,cx: &ScopeState, num: usize) {
72+
fn overwrite(&mut self, num: usize) {
7373
self.labels.clear();
74-
append(cx, &mut self.labels, num, self.last_key + 1);
74+
append(&mut self.labels, num, self.last_key + 1);
7575
self.last_key += num;
7676
}
7777

@@ -88,11 +88,9 @@ impl LabelsContainer {
8888
}
8989
}
9090

91-
fn app(cx: Scope) -> Element {
92-
let labels_container = use_ref(&cx, || LabelsContainer::new(cx, 0, 0));
93-
let selected: &Tracked<Option<usize>> = cx.use_hook(||{
94-
Tracked::new(&cx, None)
95-
});
91+
fn app() -> Element {
92+
let labels_container = use_signal(|| LabelsContainer::new(0, 0));
93+
let selected: &Signal<Option<usize>> = use_signal(|| use_signal(|| None));
9694

9795
rsx! {
9896
div { class: "container",
@@ -102,13 +100,13 @@ fn app(cx: Scope) -> Element {
102100
div { class: "col-md-6",
103101
div { class: "row",
104102
ActionButton { name: "Create 1,000 rows", id: "run",
105-
onclick: move |_| labels_container.write().overwrite(cx, 1_000),
103+
onclick: move |_| labels_container.write().overwrite( 1_000),
106104
}
107105
ActionButton { name: "Create 10,000 rows", id: "runlots",
108-
onclick: move |_| labels_container.write().overwrite(cx, 10_000),
106+
onclick: move |_| labels_container.write().overwrite( 10_000),
109107
}
110108
ActionButton { name: "Append 1,000 rows", id: "add",
111-
onclick: move |_| labels_container.write().append(cx, 1_000),
109+
onclick: move |_| labels_container.write().append( 1_000),
112110
}
113111
ActionButton { name: "Update every 10th row", id: "update",
114112
onclick: move |_| {
@@ -119,7 +117,7 @@ fn app(cx: Scope) -> Element {
119117
},
120118
}
121119
ActionButton { name: "Clear", id: "clear",
122-
onclick: move |_| labels_container.write().overwrite(cx, 0),
120+
onclick: move |_| labels_container.write().overwrite( 0),
123121
}
124122
ActionButton { name: "Swap rows", id: "swaprows",
125123
onclick: move |_| labels_container.write().swap(1, 998),
@@ -162,15 +160,16 @@ impl PartialEq for RowProps {
162160
}
163161
}
164162

165-
fn Row(cx: Scope<RowProps>) -> Element {
163+
fn Row(props: RowProps) -> Element {
166164
let RowProps {
167165
label,
168166
labels,
169167
selected_row,
170-
} = &cx.props;
171-
let label_text = use_selector(&cx, &label.label, |label| label.clone());
168+
} = &props;
169+
170+
let label_text = use_selector(&label.label, |label| label.clone());
172171
let key = label.key;
173-
let is_in_danger = use_selector(&cx, selected_row, move |selected_row: &Option<usize>| {
172+
let is_in_danger = use_selector(selected_row, move |selected_row: &Option<usize>| {
174173
let result = match selected_row {
175174
Some(selected_row) => {
176175
if *selected_row == key {
@@ -202,13 +201,8 @@ fn Row(cx: Scope<RowProps>) -> Element {
202201
}
203202
}
204203

205-
#[inline_props]
206-
fn ActionButton<'a>(
207-
cx: Scope<'a, ActionButtonProps>,
208-
name: &'static str,
209-
id: &'static str,
210-
onclick: EventHandler<'a>,
211-
) -> Element {
204+
#[component]
205+
fn ActionButton(name: &str, id: &str, onclick: EventHandler) -> Element {
212206
rsx! {
213207
div {
214208
class: "col-sm-6 smallpad",
@@ -260,4 +254,3 @@ static NOUNS: &[&str] = &[
260254
"table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
261255
"pizza", "mouse", "keyboard",
262256
];
263-

0 commit comments

Comments
 (0)