add test cases, refactor deref
This commit is contained in:
7 files changed
+137
-54
No files matched your search
@@ -0,0 +1,55 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn main() {
|
||||
launch_desktop(app);
|
||||
}
|
||||
|
||||
fn app() -> Element {
|
||||
let mut show_child = use_signal(|| true);
|
||||
let mut count = use_signal(|| 0);
|
||||
|
||||
let child = use_memo(move || {
|
||||
rsx! {
|
||||
Child {
|
||||
count
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rsx! {
|
||||
button { onclick: move |_| show_child.toggle(), "Toggle child" }
|
||||
button { onclick: move |_| count += 1, "Increment count" }
|
||||
if show_child() {
|
||||
{child.cloned()}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Child(count: Signal<i32>) -> Element {
|
||||
let mut early_return = use_signal(|| false);
|
||||
|
||||
let early = rsx! {
|
||||
button { onclick: move |_| early_return.toggle(), "Toggle {early_return} early return" }
|
||||
};
|
||||
|
||||
if early_return() {
|
||||
return early;
|
||||
}
|
||||
|
||||
use_future(move || async move {
|
||||
loop {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
println!("Child")
|
||||
}
|
||||
});
|
||||
|
||||
use_effect(move || {
|
||||
println!("Child count: {}", count());
|
||||
});
|
||||
|
||||
rsx! {
|
||||
"hellO!"
|
||||
{early}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,8 @@ fn Child(
|
||||
|
||||
println!("rendering child: {}", depth());
|
||||
|
||||
// These memos don't get re-computed when early returns happen
|
||||
// In dioxus futures spawned with use_future won't progress if they don't get hit during rendering
|
||||
let state = use_memo(move || state() + 1);
|
||||
let item = use_memo(move || items()[dbg!(depth()) - 1]);
|
||||
let depth = use_memo(move || depth() - 1);
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn main() {
|
||||
launch_desktop(app);
|
||||
}
|
||||
|
||||
fn app() -> Element {
|
||||
let mut state = use_signal(|| 0);
|
||||
let mut depth = use_signal(|| 1 as usize);
|
||||
|
||||
if depth() == 5 {
|
||||
return rsx! {
|
||||
div { "Max depth reached" }
|
||||
button { onclick: move |_| depth -= 1, "Remove depth" }
|
||||
};
|
||||
}
|
||||
|
||||
let mut items = use_memo(move || (0..depth()).map(|f| f as _).collect::<Vec<isize>>());
|
||||
|
||||
rsx! {
|
||||
button { onclick: move |_| state += 1, "Increment" }
|
||||
button { onclick: move |_| depth += 1, "Add depth" }
|
||||
button {
|
||||
onclick: move |_| async move {
|
||||
depth += 1;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
dbg!(items.read());
|
||||
// if depth() is 5, this will be the old since the memo hasn't been re-computed
|
||||
// use_memos are only re-computed when the signals they capture change
|
||||
// *and* they are used in the current render
|
||||
// If the use_memo isn't used, it can't be re-computed!
|
||||
},
|
||||
"Add depth with sleep"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user