update LocalResource note to 0.8 API

This commit is contained in:
Greg Johnston
2025-05-12 10:24:29 -04:00
committed by GitHub
parent c9fd7eb0e1
commit 708fc72648

View File

@@ -64,8 +64,6 @@ let once = OnceResource::new(load_data(42));
Both `LocalResource` and `Resource` implement the various signal access methods (`.read()`, `.with()`, `.get()`), but return `Option<T>` instead of `T`; they will be `None` until the async data has loaded.
`LocalResource` will actually return an `Option<SendWrapper<T>>`, which has to do with thread-safety requirements; you can use `.as_deref()` to get access to the inner type (see the example below).
```admonish sandbox title="Live example" collapsible=true
[Click to open CodeSandbox.](https://codesandbox.io/p/devbox/10-resource-0-7-q5xr9m?file=%2Fsrc%2Fmain.rs%3A7%2C30)
@@ -96,6 +94,56 @@ async fn load_data(value: i32) -> i32 {
value * 10
}
#[component]
pub fn App() -> impl IntoView {
// this count is our synchronous, local state
let (count, set_count) = signal(0);
// tracks `count`, and reloads by calling `load_data`
// whenever it changes
let async_data = LocalResource::new(move || load_data(count.get()));
// a resource will only load once if it doesn't read any reactive data
let stable = LocalResource::new(|| load_data(1));
// we can access the resource values with .get()
use gloo_timers::future::TimeoutFuture;
use leptos::prelude::*;
// Here we define an async function
// This could be anything: a network request, database read, etc.
// Here, we just multiply a number by 10
async fn load_data(value: i32) -> i32 {
// fake a one-second delay
TimeoutFuture::new(1_000).await;
value * 10
}
#[component]
pub fn App() -> impl IntoView {
// this count is our synchronous, local state
let (count, set_count) = signal(0);
// tracks `count`, and reloads by calling `load_data`
// whenever it changes
let async_data = LocalResource::new(move || load_data(count.get()));
// a resource will only load once if it doesn't read any reactive data
let stable = LocalResource::new(|| load_data(1));
// we can access the resource values with .get()
use gloo_timers::future::TimeoutFuture;
use leptos::prelude::*;
// Here we define an async function
// This could be anything: a network request, database read, etc.
// Here, we just multiply a number by 10
async fn load_data(value: i32) -> i32 {
// fake a one-second delay
TimeoutFuture::new(1_000).await;
value * 10
}
#[component]
pub fn App() -> impl IntoView {
// this count is our synchronous, local state
@@ -114,7 +162,6 @@ pub fn App() -> impl IntoView {
let async_result = move || {
async_data
.get()
.as_deref()
.map(|value| format!("Server returned {value:?}"))
// This loading state will only show before the first load
.unwrap_or_else(|| "Loading...".into())
@@ -127,7 +174,7 @@ pub fn App() -> impl IntoView {
"Click me"
</button>
<p>
<code>"stable"</code>": " {move || stable.get().as_deref().copied()}
<code>"stable"</code>": " {move || stable.get()}
</p>
<p>
<code>"count"</code>": " {count}