<Transition/>
You’ll notice in the <Suspense/>
example that if you keep reloading the data, it keeps flickering back to "Loading..."
. Sometimes this is fine. For other times, there’s <Transition/>
.
<Transition/>
behaves exactly the same as <Suspense/>
, but instead of falling back every time, it only shows the fallback the first time. On all subsequent loads, it continues showing the old data until the new data are ready. This can be really handy to prevent the flickering effect, and to allow users to continue interacting with your application.
This example shows how you can create a simple tabbed contact list with <Transition/>
. When you select a new tab, it continues showing the current contact until the new data loads. This can be a much better user experience than constantly falling back to a loading message.
Live example
CodeSandbox Source
use gloo_timers::future::TimeoutFuture;
use leptos::prelude::*;
async fn important_api_call(id: usize) -> String {
TimeoutFuture::new(1_000).await;
match id {
0 => "Alice",
1 => "Bob",
2 => "Carol",
_ => "User not found",
}
.to_string()
}
#[component]
fn App() -> impl IntoView {
let (tab, set_tab) = signal(0);
let (pending, set_pending) = signal(false);
// this will reload every time `tab` changes
let user_data = LocalResource::new(move || important_api_call(tab.get()));
view! {
<div class="buttons">
<button
on:click=move |_| set_tab.set(0)
class:selected=move || tab.get() == 0
>
"Tab A"
</button>
<button
on:click=move |_| set_tab.set(1)
class:selected=move || tab.get() == 1
>
"Tab B"
</button>
<button
on:click=move |_| set_tab.set(2)
class:selected=move || tab.get() == 2
>
"Tab C"
</button>
</div>
<p>
{move || if pending.get() {
"Hang on..."
} else {
"Ready."
}}
</p>
<Transition
// the fallback will show initially
// on subsequent reloads, the current child will
// continue showing
fallback=move || view! { <p>"Loading initial data..."</p> }
// this will be set to `true` whenever the transition is ongoing
set_pending
>
<p>
{move || user_data.read().as_deref().map(ToString::to_string)}
</p>
</Transition>
}
}
fn main() {
leptos::mount::mount_to_body(App)
}