Update signals example
This commit is contained in:
1 file changed
+14
-3
+14
-3
@@ -10,18 +10,29 @@ fn app() -> Element {
|
|||||||
let mut count = use_signal(|| 0);
|
let mut count = use_signal(|| 0);
|
||||||
let mut saved_values = use_signal(|| vec![0.to_string()]);
|
let mut saved_values = use_signal(|| vec![0.to_string()]);
|
||||||
|
|
||||||
// Signals can be used in async functions without an explicit clone since they're 'static and Copy
|
// use_memo will recompute the value of the signal whenever the captured signals change
|
||||||
// Signals are backed by a runtime that is designed to deeply integrate with Dioxus apps
|
let doubled_count = use_memo(move || count() * 2);
|
||||||
|
|
||||||
|
// use_effect will subscribe to any changes in the signal values it captures
|
||||||
|
// effects will always run after first mount and then whenever the signal values change
|
||||||
|
use_effect(move || println!("Count changed to {}", count()));
|
||||||
|
|
||||||
|
// use_future will spawn an infinitely running future that can be started and stopped
|
||||||
use_future(|| async move {
|
use_future(|| async move {
|
||||||
loop {
|
loop {
|
||||||
if running() {
|
if running() {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_millis(400)).await;
|
tokio::time::sleep(Duration::from_millis(400)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// use_resource will spawn a future that resolves to a value - essentially an async memo
|
||||||
|
let slow_count = use_resource(move || async move {
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
|
count() * 2
|
||||||
|
});
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
h1 { "High-Five counter: {count}" }
|
h1 { "High-Five counter: {count}" }
|
||||||
button { onclick: move |_| count += 1, "Up high!" }
|
button { onclick: move |_| count += 1, "Up high!" }
|
||||||
|
|||||||
Reference in new issue
Block a user