DioxusLabs / DioxusLabs/dioxus
Resource state not cleared on new fetch: use_resource returns stale data during loading
- Dominant language
- Rust
- Stars
- 39.1k
- Forks
- 1.9k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 4
Description
**Problem**
According to the official documentation, when a resource is triggered and a new request is made, `use_resource` should return `None` while the new request is in progress. However, in my tests, I found that when triggering a new request (e.g., by changing the input), `use_resource` immediately returns the result from the previous request, even while the new request is still pending. It does not return `None` as expected; instead, it continues to return the previous result until the new request completes.
**Steps To Reproduce**
Steps to reproduce the behavior:
1. Use the following code example in a Dioxus component:
```rust
let mut breed = use_signal(|| "hound".to_string());
let dogs = use_resource(move || async move {
gloo_timers::future::sleep(std::time::Duration::from_secs(3)).await;
reqwest::Client::new()
.get(format!("https://dog.ceo/api/breed/{breed}/images"))
.send()
.await?
.json::()
.await
});
rsx! {
input {
value: "{breed}",
oninput: move |evt| breed.set(evt.value()),
}
div {
{
if let Some(response) = &*dogs.read() {
match response {
Ok(urls) => rsx! {
for image in urls.message.iter().take(3) {
img { src: "{image}", width: "100px", height: "100px" }
}
},
Err(err) => rsx! { "Failed to fetch response: {err}" },
}
} else {
rsx! { "Loading..." }
}
}
}
}
```
2. Change the input value to trigger a new resource request.
3. Observe the UI and logs while the new request is in progress.
**Expected behavior**
I expect that when a new resource request is triggered, and the resource is still loading, `use_resource` should return `None` (i.e., show the loading state or `rsx! { "Loading..." }`). Instead, it returns the previous request's result until the new request completes.
**Screenshots**
**Environment:**
- Dioxus version: 0.6.3
- Rust version: 1.87.0
- OS info: macOS
- App platform: web, desktop
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.