Skip to content

Commit

Permalink
fix: doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DogeDark committed May 20, 2024
1 parent ac84795 commit b08e536
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
8 changes: 2 additions & 6 deletions docs-src/0.5/en/reference/event_handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,9 @@ Then, you can use it like any other handler:
> Note: just like any other attribute, you can name the handlers anything you want! Any closure you pass in will automatically be turned into an `EventHandler`.
#### Async Event Handlers
Passing `EventHandler`s as props does not support passing a closure that returns an async block:
Passing `EventHandler`s as props does not support passing a closure that returns an async block. Instead, you must manually call ``spawn`` to do async operations:
```rust, no_run
{{#include src/doc_examples/event_handler_prop.rs:async_bad}}
```
Instead, you must manually call ``spawn`` to do async operations:
```rust, no_run
{{#include src/doc_examples/event_handler_prop.rs:async_good}}
{{#include src/doc_examples/event_handler_prop.rs:async}}
```
This is only the case for custom event handlers as props.

Expand Down
27 changes: 8 additions & 19 deletions src/doc_examples/event_handler_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ fn main() {
fn App() -> Element {
// ANCHOR: usage
rsx! {
FancyButton {
onclick: move |event| println!("Clicked! {event:?}"),
"this is a button"
FancyButton {
onclick: move |event| println!("Clicked! {event:?}"),
}
}
// ANCHOR_END: usage
Expand Down Expand Up @@ -54,31 +53,21 @@ pub fn CustomFancyButton(props: CustomFancyButtonProps) -> Element {
// ANCHOR_END: custom_data

pub fn MyComponent() -> Element {
// ANCHOR: async_bad
// ANCHOR: async
rsx! {
FancyButton {
// This does not work!
onclick: move |event| async move {
println!("Clicked! {event:?}");
},
"this is a button"
}
}
// ANCHOR_END: async_bad
}
// onclick: move |event| async move {
// println!("Clicked! {event:?}");
// },

pub fn MyComponent() -> Element {
// ANCHOR: async_good
rsx! {
FancyButton {
// This works!
// This does work!
onclick: move |event| {
spawn(async move {
println!("Clicked! {event:?}");
});
},
"this is a button"
}
}
// ANCHOR_END: async_good
// ANCHOR_END: async
}

0 comments on commit b08e536

Please sign in to comment.