dioxus/docs/guide/examples/spawn.rs
Reinis Mazeiks 6c323e9fc5
Rework Guide (#486)
* Move getting started to Guide

* Clean up summary and getting started

* Shorten intro by moving stuff to individual platform docs

* Make intro even shorter, summarize features

* Further cleanup; move development-related docs to separate section

* Make `guide` a crate. Turn code samples into examples so that we can check if they compile

* Rewrite "Describing the UI":

* Focus on RSX syntax (interactivity covered in later chapters); make sure samples are tested; concise language

* Move some "special attribute" samples to the `examples` directory

* Simplify introduction to components

* Simplify introduction to component props

* Document Prop features; add code samples

* Simplify component children docs

* Interactivity: better introduction to events

* Hooks: better introduction

* Remove outdated doc

* Introducs use_ref

* Simplify User Input chapter

* Document event handler props

* Meme editor example

* Meme editor walkthrough

* Add dark mode example

* Guide for context; dark mode example

* Guide: custom hooks

* Guide: conditional rendering

* Guide: rendering lists

* Guide: rendering lists + keys

* Move remaining infor from Reference to guide

* Delete reference book
2022-07-07 08:50:36 +00:00

84 lines
1.9 KiB
Rust

#![allow(non_snake_case, unused)]
use dioxus::prelude::*;
fn main() {
dioxus::desktop::launch(App);
}
fn App(cx: Scope) -> Element {
// ANCHOR: spawn
let logged_in = use_state(&cx, || false);
let log_in = move |_| {
cx.spawn({
let logged_in = logged_in.to_owned();
async move {
let resp = reqwest::Client::new()
.post("http://example.com/login")
.send()
.await;
match resp {
Ok(_data) => {
println!("Login successful!");
logged_in.set(true);
}
Err(_err) => {
println!(
"Login failed - you need a login server running on localhost:8080."
)
}
}
}
});
};
cx.render(rsx! {
button {
onclick: log_in,
"Login",
}
})
// ANCHOR_END: spawn
}
pub fn Tokio(cx: Scope) -> Element {
let _ = || {
// ANCHOR: tokio
cx.spawn(async {
let _ = tokio::spawn(async {}).await;
let _ = tokio::task::spawn_local(async {
// some !Send work
})
.await;
});
// ANCHOR_END: tokio
};
None
}
pub fn ToOwnedMacro(cx: Scope) -> Element {
let count = use_state(&cx, || 0);
let age = use_state(&cx, || 0);
let name = use_state(&cx, || 0);
let description = use_state(&cx, || 0);
let _ = || {
// ANCHOR: to_owned_macro
use dioxus::core::to_owned;
cx.spawn({
to_owned![count, age, name, description];
async move {
// ...
}
});
// ANCHOR_END: to_owned_macro
};
None
}