fix signal readme example

This commit is contained in:
Evan Almloff committed 2023-08-08 11:12:08 -07:00
1 parent 292b3367dd
commit e2c08a79ae
2 files changed
+17 -5

No files matched your search

+16 -4
View File
@@ -7,12 +7,15 @@ Dioxus Signals is an ergonomic Copy runtime for data with local subscriptions in
All signals implement Copy, even if the inner value does not implement copy. This makes it easy to move any data into futures or children.
```rust
fn App(cx: Scope) -> Element {
use dioxus::prelude::*;
use dioxus_signals::*;
fn app(cx: Scope) -> Element {
let signal = use_signal(cx, || "hello world".to_string());
spawn(async move {
// signal is Copy even though String is not copy
signal
print!("{signal}");
});
render! {
@@ -26,13 +29,19 @@ fn App(cx: Scope) -> Element {
Signals will only subscribe to components when you read from the signal in that component. It will never subscribe to a component when reading data in a future or event handler.
```rust
use dioxus::prelude::*;
use dioxus_signals::*;
fn app(cx: Scope) -> Element {
// Because signal is never read in this component, this component will not rerun when the signal changes
let signal = use_signal(cx, || 0);
render! {
onclick: move |_| {
*signal.write() += 1;
button {
onclick: move |_| {
*signal.write() += 1;
},
"Increase"
}
for id in 0..10 {
Child {
@@ -58,6 +67,9 @@ fn Child(cx: Scope<ChildProps>) -> Element {
Because subscriptions happen when you read from (not create) the data, you can provide signals through the normal context API:
```rust
use dioxus::prelude::*;
use dioxus_signals::*;
fn app(cx: Scope) -> Element {
// Because signal is never read in this component, this component will not rerun when the signal changes
use_context_provider(cx, || Signal::new(0));
+1 -1
View File
@@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_signals::{use_signal, Effect, Signal};
use dioxus_signals::Signal;
fn main() {
dioxus_desktop::launch(app);