How to handle form submission / async events / closures #406
Answered
by
lukechu10
seanpmyers
asked this question in
Q&A
-
Hello, I'm trying to implement a basic form, but I wasn't sure the best way to handle the submission since it's async. #[component]
pub async fn RegistrationForm<G: Html>(context: Scope<'_>) -> View<G> {
let email_address: &Signal<String> = create_signal(context, String::new());
let password: &Signal<String> = create_signal(context, String::new());
let submit_registration = |_| async move {
if email_address.get().is_empty() || password.get().is_empty() {
return;
}
let response = register_user(
email_address.get().as_ref().clone(),
password.get().as_ref().clone(),
)
.await
.unwrap_or_default();
};
view! { context,
form {
div {
label {"Email Address"}
input(placeholder="example@email.com", type="text", bind=email_address.clone())
}
div {
label {"Password"}
input(placeholder="", type="password", bind=password.clone())
}
div {
button(type="submit", on:submit=submit_registration)
}
}
}
} Any input would be appreciated. Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
lukechu10
Apr 12, 2022
Replies: 1 comment 2 replies
-
You can't create async event handlers yet. This is planned but for now, you need to use let submit_registration = |_| spawn_local_scoped(context, async move {
if email_address.get().is_empty() || password.get().is_empty() {
return;
}
let response = register_user(
email_address.get().as_ref().clone(),
password.get().as_ref().clone(),
)
.await
.unwrap_or_default();
}); Make sure you enable |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
seanpmyers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can't create async event handlers yet. This is planned but for now, you need to use
spawn_local_scoped
from inside a normal event handler.Make sure you enable
"suspense"
feature onsycamore
.