add selector example to readme
This commit is contained in:
1 parent
ba7a663eb6
commit
69dab86873
4 files changed
+65
-3
No files matched your search
@@ -245,7 +245,7 @@ impl ScopeContext {
|
|||||||
|
|
||||||
/// Schedule an update for any component given its [`ScopeId`].
|
/// 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
|
/// 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>> {
|
pub fn schedule_update_any() -> Option<Arc<dyn Fn(ScopeId) + Send + Sync>> {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Dioxus Signals
|
# 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
|
## Copy Data
|
||||||
|
|
||||||
@@ -86,5 +86,37 @@ fn Child(cx: Scope) -> Element {
|
|||||||
"{signal}"
|
"{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}"
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -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}"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -87,7 +87,7 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
|
|||||||
value: f(),
|
value: f(),
|
||||||
});
|
});
|
||||||
{
|
{
|
||||||
// get_effect_stack().effects.borrow_mut().pop();
|
get_effect_stack().effects.borrow_mut().pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
effect.callback.value.set(Box::new(move || {
|
effect.callback.value.set(Box::new(move || {
|
||||||
|
|||||||
Reference in new issue
Block a user