mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
add selector example to readme
This commit is contained in:
parent
ba7a663eb6
commit
69dab86873
4 changed files with 66 additions and 4 deletions
|
@ -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>> {
|
||||
|
|
|
@ -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
packages/signals/examples/selector.rs
Normal file
30
packages/signals/examples/selector.rs
Normal file
|
@ -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(),
|
||||
});
|
||||
{
|
||||
// get_effect_stack().effects.borrow_mut().pop();
|
||||
get_effect_stack().effects.borrow_mut().pop();
|
||||
}
|
||||
|
||||
effect.callback.value.set(Box::new(move || {
|
||||
|
|
Loading…
Reference in a new issue