GithubHelp home page GithubHelp logo

Comments (3)

jetli avatar jetli commented on June 8, 2024

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.

ctron avatar ctron commented on June 8, 2024

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.

ctron avatar ctron commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.