use rscx::{
component,
context::{provide_context, spawn_local, use_context},
html, props,
};
use tokio::task::LocalSet;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let local = LocalSet::new();
let res = local
.run_until(async move {
spawn_local(async {
provide_context("John".to_string());
app().await
})
.await
})
.await
.unwrap();
println!("{}", res);
Ok(())
}
async fn app() -> String {
html! {
<!DOCTYPE html>
<div>
<Greetings />
</div>
}
}
#[component]
async fn Greetings() -> String {
let current_user = use_context::<String>();
html! {
<p>
{
if let Some(user) = current_user {
format!("Hello, {}!", user)
} else {
"Log in first!".to_string()
}
}
</p>
}
}