Ver Fonte

add selector example to readme

Evan Almloff há 1 ano atrás
pai
commit
69dab86873

+ 1 - 1
packages/core/src/scope_context.rs

@@ -245,7 +245,7 @@ impl ScopeContext {
 
 /// Schedule an update for any component given its [`ScopeId`].
 ///
-/// A component's [`ScopeId`] can be obtained from `use_hook` or the [`ScopeState::scope_id`] method.
+/// A component's [`ScopeId`] can be obtained from `use_hook` or the [`crate::scopes::ScopeState::scope_id`] method.
 ///
 /// This method should be used when you want to schedule an update for a component
 pub fn schedule_update_any() -> Option<Arc<dyn Fn(ScopeId) + Send + Sync>> {

+ 33 - 1
packages/signals/README.md

@@ -1,6 +1,6 @@
 # Dioxus Signals
 
-Dioxus Signals is an ergonomic Copy runtime for data with local subscriptions in Dioxus.
+Dioxus Signals is an ergonomic Copy runtime for data with local subscriptions.
 
 ## Copy Data
 
@@ -86,5 +86,37 @@ fn Child(cx: Scope) -> Element {
         "{signal}"
     }
 }
+```
+
+## Computed Data
+
+In addition to local subscriptions in components, `dioxus-signals` provides a way to derive data with local subscriptions.
+
+The use_selector hook will only rerun when any signals inside of the hook change:
+
+```rust
+use dioxus::prelude::*;
+use dioxus_signals::*;
+
+fn app(cx: Scope) -> Element {
+    let signal = use_signal(cx, || 0);
+    let doubled = use_selector(cx, || signal * 2);
 
+    render! {
+        button {
+            onclick: move |_| *signal.write() += 1,
+            "Increase"
+        }
+        Child {
+            signal: signal
+        }
+    }
+}
+
+#[inline_props]
+fn Child(cx: Scope, signal: ReadOnlySignal<usize>) -> Element {
+    render! {
+        "{signal}"
+    }
+}
 ```

+ 30 - 0
packages/signals/examples/selector.rs

@@ -0,0 +1,30 @@
+#![allow(non_snake_case)]
+
+use dioxus::prelude::*;
+use dioxus_signals::*;
+
+fn main() {
+    dioxus_desktop::launch(app);
+}
+
+fn app(cx: Scope) -> Element {
+    let signal = use_signal(cx, || 0);
+    let doubled = use_selector(cx, move || signal * 2);
+
+    render! {
+        button {
+            onclick: move |_| *signal.write() += 1,
+            "Increase"
+        }
+        Child {
+            signal: doubled
+        }
+    }
+}
+
+#[inline_props]
+fn Child(cx: Scope, signal: ReadOnlySignal<usize>) -> Element {
+    render! {
+        "{signal}"
+    }
+}

+ 1 - 1
packages/signals/src/selector.rs

@@ -87,7 +87,7 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
         value: f(),
     });
     {
-        // get_effect_stack().effects.borrow_mut().pop();
+        get_effect_stack().effects.borrow_mut().pop();
     }
 
     effect.callback.value.set(Box::new(move || {