State syncing

This commit is contained in:
2026-01-26 17:27:47 +01:00
parent 86e3f4e684
commit 506ccae049
2 changed files with 26 additions and 29 deletions
+4 -29
View File
@@ -13,6 +13,9 @@ fn app(cx: &Arc<Context>) -> View {
let count = use_state(0);
let mount = use_mount_manual();
// Sync state with children
use_sync_effect(cx, &count, |v| Count(v.clone()), &[&mount, &count]);
use_effect(
{
let count = count.clone();
@@ -29,18 +32,6 @@ fn app(cx: &Arc<Context>) -> View {
&[&mount],
);
// Pass the count to the children
use_effect(
{
let count = count.clone();
let cx = cx.clone();
move || {
cx.emit_event(&Count(count.clone()));
}
},
&[&mount],
);
rsx! {
// Static scope
my_component (ctx, view) {
@@ -67,25 +58,9 @@ fn app(cx: &Arc<Context>) -> View {
}
fn my_component(cx: &Arc<Context>) -> View {
let mount = use_mount();
// Sync state with parent
let count = use_sync_state(cx, 0, |Count(v)| v.get_dl());
use_effect(
{
let count = count.clone();
move || loop {
std::thread::sleep(std::time::Duration::from_millis(500));
let mut count = count.get();
if *count > 5 {
*count = 0;
continue;
}
*count += 1;
}
},
&[&mount],
);
rsx! {
"Count: {count}"
}