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

Hooks: Change UseState to be like react's use_state #129

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
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
36 changes: 19 additions & 17 deletions examples/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let display_value: UseState<String> = use_state(&cx, || String::from("0"));
let (display_value, set_display_value) = use_state(&cx, || String::from("0"));

let input_digit = move |num: u8| {
if display_value.get() == "0" {
display_value.set(String::new());
if display_value == "0" {
set_display_value(String::new());
}
display_value.modify().push_str(num.to_string().as_str());
set_display_value
.make_mut()
.push_str(num.to_string().as_str());
};

let input_operator = move |key: &str| {
display_value.modify().push_str(key);
set_display_value.make_mut().push_str(key);
};

cx.render(rsx!(
Expand All @@ -53,7 +55,7 @@ fn app(cx: Scope) -> Element {
KeyCode::Num9 => input_digit(9),
KeyCode::Backspace => {
if !display_value.len() != 0 {
display_value.modify().pop();
set_display_value.make_mut().pop();
}
}
_ => {}
Expand All @@ -65,30 +67,30 @@ fn app(cx: Scope) -> Element {
button {
class: "calculator-key key-clear",
onclick: move |_| {
display_value.set(String::new());
if display_value != "" {
display_value.set("0".into());
set_display_value(String::new());
if !display_value.is_empty(){
set_display_value("0".into());
}
},
[if display_value == "" { "C" } else { "AC" }]
[if display_value.is_empty() { "C" } else { "AC" }]
}
button {
class: "calculator-key key-sign",
onclick: move |_| {
let temp = calc_val(display_value.get().clone());
let temp = calc_val(display_value.clone());
if temp > 0.0 {
display_value.set(format!("-{}", temp));
set_display_value(format!("-{}", temp));
} else {
display_value.set(format!("{}", temp.abs()));
set_display_value(format!("{}", temp.abs()));
}
},
"±"
}
button {
class: "calculator-key key-percent",
onclick: move |_| {
display_value.set(
format!("{}", calc_val(display_value.get().clone()) / 100.0)
set_display_value(
format!("{}", calc_val(display_value.clone()) / 100.0)
);
},
"%"
Expand All @@ -98,7 +100,7 @@ fn app(cx: Scope) -> Element {
button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
"0"
}
button { class: "calculator-key key-dot", onclick: move |_| display_value.modify().push('.'),
button { class: "calculator-key key-dot", onclick: move |_| set_display_value.make_mut().push('.'),
"●"
}
(1..10).map(|k| rsx!{
Expand Down Expand Up @@ -130,7 +132,7 @@ fn app(cx: Scope) -> Element {
}
button { class: "calculator-key key-equals",
onclick: move |_| {
display_value.set(format!("{}", calc_val(display_value.get().clone())));
set_display_value(format!("{}", calc_val(display_value.clone())));
},
"="
}
Expand Down
30 changes: 15 additions & 15 deletions examples/crm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub struct Client {
}

fn app(cx: Scope) -> Element {
let scene = use_state(&cx, || Scene::ClientsList);
let clients = use_ref(&cx, || vec![] as Vec<Client>);
let firstname = use_state(&cx, String::new);
let lastname = use_state(&cx, String::new);
let description = use_state(&cx, String::new);
let (scene, set_scene) = use_state(&cx, || Scene::ClientsList);
let (firstname, set_firstname) = use_state(&cx, String::new);
let (lastname, set_lastname) = use_state(&cx, String::new);
let (description, set_description) = use_state(&cx, String::new);

cx.render(rsx!(
body {
Expand All @@ -38,7 +38,7 @@ fn app(cx: Scope) -> Element {

h1 {"Dioxus CRM Example"}

match *scene {
match scene {
Scene::ClientsList => rsx!(
div { class: "crm",
h2 { margin_bottom: "10px", "List of clients" }
Expand All @@ -51,8 +51,8 @@ fn app(cx: Scope) -> Element {
})
)
}
button { class: "pure-button pure-button-primary", onclick: move |_| scene.set(Scene::NewClientForm), "Add New" }
button { class: "pure-button", onclick: move |_| scene.set(Scene::Settings), "Settings" }
button { class: "pure-button pure-button-primary", onclick: move |_| set_scene(Scene::NewClientForm), "Add New" }
button { class: "pure-button", onclick: move |_| set_scene(Scene::Settings), "Settings" }
}
),
Scene::NewClientForm => rsx!(
Expand All @@ -63,19 +63,19 @@ fn app(cx: Scope) -> Element {
class: "new-client firstname",
placeholder: "First name",
value: "{firstname}",
oninput: move |e| firstname.set(e.value.clone())
oninput: move |e| set_firstname(e.value.clone())
}
input {
class: "new-client lastname",
placeholder: "Last name",
value: "{lastname}",
oninput: move |e| lastname.set(e.value.clone())
oninput: move |e| set_lastname(e.value.clone())
}
textarea {
class: "new-client description",
placeholder: "Description",
value: "{description}",
oninput: move |e| description.set(e.value.clone())
oninput: move |e| set_description(e.value.clone())
}
}
button {
Expand All @@ -86,13 +86,13 @@ fn app(cx: Scope) -> Element {
first_name: (*firstname).clone(),
last_name: (*lastname).clone(),
});
description.set(String::new());
firstname.set(String::new());
lastname.set(String::new());
set_description(String::new());
set_firstname(String::new());
set_lastname(String::new());
},
"Add New"
}
button { class: "pure-button", onclick: move |_| scene.set(Scene::ClientsList),
button { class: "pure-button", onclick: move |_| set_scene(Scene::ClientsList),
"Go Back"
}
}
Expand All @@ -108,7 +108,7 @@ fn app(cx: Scope) -> Element {
}
button {
class: "pure-button pure-button-primary",
onclick: move |_| scene.set(Scene::ClientsList),
onclick: move |_| set_scene(Scene::ClientsList),
"Go Back"
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/disabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let disabled = use_state(&cx, || false);
let (disabled, set_disabled) = use_state(&cx, || false);

cx.render(rsx! {
div {
button {
onclick: move |_| disabled.set(!disabled.get()),
onclick: move |_| set_disabled(!disabled),
"click to " [if *disabled {"enable"} else {"disable"} ] " the lower button"
}

Expand Down
12 changes: 6 additions & 6 deletions examples/dog_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn app(cx: Scope) -> Element {
.await
});

let selected_breed = use_state(&cx, || None);
let (breed, set_breed) = use_state(&cx, || None);

match fut.value() {
Some(Ok(breeds)) => cx.render(rsx! {
Expand All @@ -36,14 +36,14 @@ fn app(cx: Scope) -> Element {
breeds.message.keys().map(|breed| rsx!(
li {
button {
onclick: move |_| selected_breed.set(Some(breed.clone())),
onclick: move |_| set_breed(Some(breed.clone())),
"{breed}"
}
}
))
}
div { flex: "50%",
match &*selected_breed {
match breed {
Some(breed) => rsx!( Breed { breed: breed.clone() } ),
None => rsx!("No Breed selected"),
}
Expand Down Expand Up @@ -73,9 +73,9 @@ fn Breed(cx: Scope, breed: String) -> Element {
reqwest::get(endpoint).await.unwrap().json::<DogApi>().await
});

let breed_name = use_state(&cx, || breed.clone());
if breed_name.get() != breed {
breed_name.set(breed.clone());
let (name, set_name) = use_state(&cx, || breed.clone());
if name != breed {
set_name(breed.clone());
fut.restart();
}

Expand Down
4 changes: 2 additions & 2 deletions examples/framework_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Label {

fn app(cx: Scope) -> Element {
let items = use_ref(&cx, Vec::new);
let selected = use_state(&cx, || None);
let (selected, set_selected) = use_state(&cx, || None);

cx.render(rsx! {
div { class: "container",
Expand Down Expand Up @@ -71,7 +71,7 @@ fn app(cx: Scope) -> Element {
rsx!(tr { class: "{is_in_danger}",
td { class:"col-md-1" }
td { class:"col-md-1", "{item.key}" }
td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
td { class:"col-md-1", onclick: move |_| set_selected(Some(id)),
a { class: "lbl", item.labels }
}
td { class: "col-md-1",
Expand Down
4 changes: 2 additions & 2 deletions examples/hydration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let val = use_state(&cx, || 0);
let (val, set_val) = use_state(&cx, || 0);

cx.render(rsx! {
div {
h1 { "hello world. Count: {val}" }
button {
onclick: move |_| *val.modify() += 1,
onclick: move |_| set_val(val + 1),
"click to increment"
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/pattern_reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let state = use_state(&cx, PlayerState::new);
let (state, set_state) = use_state(&cx, PlayerState::new);

cx.render(rsx!(
div {
h1 {"Select an option"}
h3 { "The radio is... " [state.is_playing()] "!" }
button { onclick: move |_| state.modify().reduce(PlayerAction::Pause),
button { onclick: move |_| set_state.make_mut().reduce(PlayerAction::Pause),
"Pause"
}
button { onclick: move |_| state.modify().reduce(PlayerAction::Play),
button { onclick: move |_| set_state.make_mut().reduce(PlayerAction::Play),
"Play"
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let mut count = use_state(&cx, || 0);
let (count, set_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 |_| set_count(count + 1), "Up high!" }
button { onclick: move |_| set_count(count - 1), "Down low!" }
}
})
}
2 changes: 1 addition & 1 deletion examples/rsx_compile_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
}

fn example(cx: Scope) -> Element {
let items = use_state(&cx, || {
let (items, _set_items) = use_state(&cx, || {
vec![Thing {
a: "asd".to_string(),
b: 10,
Expand Down
8 changes: 4 additions & 4 deletions examples/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let count = use_state(&cx, || 0);
let (count, set_count) = use_state(&cx, || 0);

use_future(&cx, move || {
let mut count = count.for_async();
let set_count = set_count.to_owned();
async move {
loop {
tokio::time::sleep(Duration::from_millis(1000)).await;
count += 1;
set_count.modify(|f| f + 1);
}
}
});
Expand All @@ -26,7 +26,7 @@ fn app(cx: Scope) -> Element {
div {
h1 { "Current count: {count}" }
button {
onclick: move |_| count.set(0),
onclick: move |_| set_count(0),
"Reset the count"
}
}
Expand Down
Loading