Merge pull request #431 from AOx0/master
Fix outdated guide section (React-like foo, set_foo)
This commit is contained in:
1 file changed
+9
-9
@@ -64,7 +64,7 @@ The most common hook you'll use for storing state is `use_state`. `use_state` pr
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn App(cx: Scope)-> Element {
|
fn App(cx: Scope)-> Element {
|
||||||
let (post, set_post) = use_state(&cx, || {
|
let post = use_state(&cx, || {
|
||||||
PostData {
|
PostData {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
score: 10,
|
score: 10,
|
||||||
@@ -112,11 +112,11 @@ For example, let's say we provide a button to generate a new post. Whenever the
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn App(cx: Scope)-> Element {
|
fn App(cx: Scope)-> Element {
|
||||||
let (post, set_post) = use_state(&cx, || PostData::new());
|
let post = use_state(&cx, || PostData::new());
|
||||||
|
|
||||||
cx.render(rsx!{
|
cx.render(rsx!{
|
||||||
button {
|
button {
|
||||||
onclick: move |_| set_post(PostData::random())
|
onclick: move |_| post.set(PostData::random())
|
||||||
"Generate a random post"
|
"Generate a random post"
|
||||||
}
|
}
|
||||||
Post { props: &post }
|
Post { props: &post }
|
||||||
@@ -141,19 +141,19 @@ We can use tasks in our components to build a tiny stopwatch that ticks every se
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn App(cx: Scope)-> Element {
|
fn App(cx: Scope)-> Element {
|
||||||
let (elapsed, set_elapsed) = use_state(&cx, || 0);
|
let elapsed = use_state(&cx, || 0);
|
||||||
|
|
||||||
use_future(&cx, || {
|
use_future(&cx, (), |()| {
|
||||||
to_owned![set_elapsed]; // explicitly capture this hook for use in async
|
to_owned![elapsed]; // explicitly capture this hook for use in async
|
||||||
async move {
|
async move {
|
||||||
loop {
|
loop {
|
||||||
TimeoutFuture::from_ms(1000).await;
|
gloo_timers::future::TimeoutFuture::new(1_000).await;
|
||||||
set_elapsed.modify(|i| i + 1)
|
elapsed.modify(|i| i + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
rsx!(cx, div { "Current stopwatch time: {sec_elapsed}" })
|
rsx!(cx, div { "Current stopwatch time: {elapsed}" })
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user