Comments (3)
hi, the design of separate fields for loading/data/error is a bit like SWR lib of js world, data and error could even co-exist for cache/user-friendly reasons, maybe we could not reduce them to a enum like Result<Data, Error>.
from yew-hooks.
Right now evaluating the state looks a bit like this:
#[function_component(EvalView)]
pub fn eval_view(props: &EvalViewProps) -> Html {
match &props.eval {
UseAsyncState { loading: true, .. } => {
html!("Loading...")
}
UseAsyncState {
error: Some(err), ..
} => {
html!(format!("Failed: {err}"))
}
UseAsyncState {
data: Some(result), ..
} => {
html!(
<ResultView result={result.clone()}/>
)
}
_ => html!(""),
}
}
Which isn't optimal IMHO. I think this could be changed into something like:
#[function_component(EvalView)]
pub fn eval_view(props: &EvalViewProps) -> Html {
match &props.eval {
UseAsyncState::Loading => {
html!("Loading...")
}
UseAsyncState::Err(err) => {
html!(format!("Failed: {err}"))
}
UseAsyncState::Ok(result) => {
html!(
<ResultView result={result.clone()}/>
)
}
}
}
Which feels a bit more natural to me. I am not sure in which case one would have all three values together. Maybe I don't understand the idea having having both data and error at the same time.
from yew-hooks.
I went ahead, trying to create an example of what I had in mind, something like:
#[derive(PartialEq, Eq)]
pub enum UseAsyncState<T, E> {
/// Operation has not been started yet
Pending,
/// Operation is running
Loading,
/// Operation is complete
Ready(Result<T, E>),
}
While updating the code I think I ran into what you had in mind with "caching", when updating the example.
When the state changes to "loading" again, the old data is still available. So data can be shown, while loading new.
I personally am not a fan of this, as it shows stale data. Maybe the solution could be to create a second version of use_async
, which doesn't have this have this behavior. And maybe one implementation can be built on the other.
from yew-hooks.
Related Issues (16)
- Renaming websocket breaks compilation
- How to store state from use_async via use_local_storage? HOT 2
- use_async in combination with use_memo HOT 2
- use_async with a parameter from state HOT 2
- Can't access websocket from within callbacks
- UseClipboardHandle's write_text is not working properly on Chrome on Mac. HOT 4
- `use_drop` returns stale data
- Allow using multiple nodes with use_click_away HOT 1
- Nested State
- Bug in use_drop with nested target
- use_async error (the trait bound `impl yew::functional::hooks::Hook<Output = UseAsyncHandle<_, _>> + '_: Hook` is not satisfied) HOT 1
- Upgrade to yew 0.21
- unexpected behavior of use_debounce
- UseAsyncOptions::enable_auto() can lead to none of the options set HOT 2
- `use_websocket_with_options`, unintuitive reconnection_limit and unreliable reconnection HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yew-hooks.