-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
lib.rs
52 lines (48 loc) · 2.03 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use leptos::prelude::*;
#[component]
pub fn App() -> impl IntoView {
let (value, set_value) = signal("".parse::<i32>());
view! {
<h1>"Error Handling"</h1>
<label>
"Type a number (or something that's not a number!)"
<input
type="text"
value=move || value.get().unwrap_or_default()
// when input changes, try to parse a number from the input
on:input:target=move |ev| set_value.set(ev.target().value().parse::<i32>())
/> // If an `Err(_) has been rendered inside the <ErrorBoundary/>,
// the fallback will be displayed. Otherwise, the children of the
// <ErrorBoundary/> will be displayed.
// the fallback receives a signal containing current errors
<ErrorBoundary fallback=|errors| {
let errors = errors.clone();
view! {
<div class="error">
<p>"Not a number! Errors: "</p>
// we can render a list of errors
// as strings, if we'd like
<ul>
{move || {
errors
.read()
.iter()
.map(|(_, e)| view! { <li>{e.to_string()}</li> })
.collect::<Vec<_>>()
}}
</ul>
</div>
}
}>
<p>
// because `value` is `Result<i32, _>`,
// it will render the `i32` if it is `Ok`,
"You entered " // and render nothing and trigger the error boundary
// if it is `Err`. It's a signal, so this will dynamically
// update when `value` changes
<strong>{value}</strong>
</p>
</ErrorBoundary>
</label>
}
}