Sfoglia il codice sorgente

fix memo chain example

Evan Almloff 1 anno fa
parent
commit
da4d9c70e8
1 ha cambiato i file con 7 aggiunte e 7 eliminazioni
  1. 7 7
      examples/memo_chain.rs

+ 7 - 7
examples/memo_chain.rs

@@ -21,25 +21,25 @@ fn app() -> Element {
         button { onclick: move |_| value += 1, "Increment" }
         button { onclick: move |_| depth += 1, "Add depth" }
         button { onclick: move |_| depth -= 1, "Remove depth" }
-        Child { depth, items, state }
+        if depth() > 0 {
+            Child { depth, items, state }
+        }
     }
 }
 
 #[component]
 fn Child(state: Memo<isize>, items: Memo<Vec<isize>>, depth: ReadOnlySignal<usize>) -> Element {
-    if depth() == 0 {
-        return None;
-    }
-
     // These memos don't get re-computed when early returns happen
     let state = use_memo(move || state() + 1);
-    let item = use_memo(move || items()[depth()]);
+    let item = use_memo(move || items()[depth() - 1]);
     let depth = use_memo(move || depth() - 1);
 
     println!("rendering child: {}", depth());
 
     rsx! {
         h3 { "Depth({depth})-Item({item}): {state}"}
-        Child { depth, state, items }
+        if depth() > 0 {
+            Child { depth, state, items }
+        }
     }
 }