update: modify usestate to be borrowed

This commit is contained in:
Jonathan Kelley committed 2022-01-16 15:56:48 -05:00
1 parent 8e842231e4
commit 58839f47ba
10 files changed
+237 -419

No files matched your search

+3 -3
View File
@@ -18,7 +18,7 @@ fn main() {
}
fn app(cx: Scope) -> Element {
let display_value: UseState<String> = use_state(&cx, || String::from("0"));
let display_value = use_state(&cx, || String::from("0"));
let input_digit = move |num: u8| {
if display_value.get() == "0" {
@@ -66,11 +66,11 @@ fn app(cx: Scope) -> Element {
class: "calculator-key key-clear",
onclick: move |_| {
display_value.set(String::new());
if display_value != "" {
if *display_value != "" {
display_value.set("0".into());
}
},
[if display_value == "" { "C" } else { "AC" }]
[if *display_value == "" { "C" } else { "AC" }]
}
button {
class: "calculator-key key-sign",
+1 -1
View File
@@ -38,7 +38,7 @@ fn app(cx: Scope) -> Element {
h1 {"Dioxus CRM Example"}
match *scene {
match scene.get() {
Scene::ClientsList => rsx!(
div { class: "crm",
h2 { margin_bottom: "10px", "List of clients" }
+1 -1
View File
@@ -11,7 +11,7 @@ fn app(cx: Scope) -> Element {
div {
button {
onclick: move |_| disabled.set(!disabled.get()),
"click to " [if *disabled {"enable"} else {"disable"} ] " the lower button"
"click to " [if **disabled {"enable"} else {"disable"} ] " the lower button"
}
button {
+1 -1
View File
@@ -42,7 +42,7 @@ fn app(cx: Scope) -> Element {
))
}
div { flex: "50%",
match &*selected_breed {
match selected_breed.get() {
Some(breed) => rsx!( Breed { breed: breed.clone() } ),
None => rsx!("No Breed selected"),
}
+3 -3
View File
@@ -9,13 +9,13 @@ fn main() {
}
fn app(cx: Scope) -> Element {
let mut count = use_state(&cx, || 0);
let count = use_state(&cx, || 0);
cx.render(rsx! {
div {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button { onclick: move |_| *count.modify() += 1, "Up high!" }
button { onclick: move |_| *count.modify() -= 1, "Down low!" }
}
})
}
+16 -18
View File
@@ -19,15 +19,15 @@ pub struct TodoItem {
}
pub fn app(cx: Scope<()>) -> Element {
let todos = use_state(&cx, || im_rc::HashMap::<u32, TodoItem>::default());
let todos = use_state(&cx, im_rc::HashMap::<u32, TodoItem>::default);
let filter = use_state(&cx, || FilterState::All);
let draft = use_state(&cx, || "".to_string());
let mut todo_id = use_state(&cx, || 0);
let (todo_id, set_todo_id) = use_state(&cx, || 0).split();
// Filter the todos based on the filter state
let mut filtered_todos = todos
.iter()
.filter(|(_, item)| match *filter {
.filter(|(_, item)| match **filter {
FilterState::All => true,
FilterState::Active => !item.checked,
FilterState::Completed => item.checked,
@@ -56,19 +56,17 @@ pub fn app(cx: Scope<()>) -> Element {
autofocus: "true",
oninput: move |evt| draft.set(evt.value.clone()),
onkeydown: move |evt| {
if evt.key == "Enter" {
if !draft.is_empty() {
todos.modify().insert(
*todo_id,
TodoItem {
id: *todo_id,
checked: false,
contents: draft.get().clone(),
},
);
todo_id += 1;
draft.set("".to_string());
}
if evt.key == "Enter" && !draft.is_empty() {
todos.modify().insert(
*todo_id,
TodoItem {
id: *todo_id,
checked: false,
contents: draft.get().clone(),
},
);
set_todo_id(todo_id + 1);
draft.set("".to_string());
}
}
}
@@ -90,7 +88,7 @@ pub fn app(cx: Scope<()>) -> Element {
(show_clear_completed).then(|| rsx!(
button {
class: "clear-completed",
onclick: move |_| todos.modify().retain(|_, todo| todo.checked == false),
onclick: move |_| todos.modify().retain(|_, todo| !todo.checked),
"Clear completed"
}
))
@@ -108,7 +106,7 @@ pub fn app(cx: Scope<()>) -> Element {
#[derive(Props)]
pub struct TodoEntryProps<'a> {
todos: UseState<'a, im_rc::HashMap<u32, TodoItem>>,
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
id: u32,
}