Skip to content

Possible Bug in Chapter 3.3? #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seemawn1981 opened this issue Apr 9, 2025 · 2 comments
Closed

Possible Bug in Chapter 3.3? #185

seemawn1981 opened this issue Apr 9, 2025 · 2 comments

Comments

@seemawn1981
Copy link

seemawn1981 commented Apr 9, 2025

Hi,
I am studying the book, and in Chapter 3.3, the code after "if you are savvy..." leads to this:

#[component]
fn ProgressBar(
    #[prop(default = 100)] max: u16,
    progress: impl Fn() -> i32 + Send + Sync + 'static,
) -> impl IntoView {
    view! {
        <progress
            max=max
            value=progress
        />
        // Add a line-break to avoid overlap
        <br/>
    }
}
#[component]
fn App() -> impl IntoView {
    let (count, set_count) = signal(0);
    // Defining the closure that accesses the signal
    let double_count = move || count.get() * 2;
    view! {
        <button
            on:click=move |_| {
                set_count.update(|n| *n +=1 );
            }
            >
            "Click me"
        </button>
        // Progress Bar with Signal
        <ProgressBar progress = count> // <--- Fails here
        <ProgressBar progress = double_count/>
    }
}

Trunk serve fails to compile because "expected an Fn() closure, found leptos::prelude::ReadSignal<{integer}>"
To make sure that I was not mistaken, I just copied and pasted the code for the progress bar (the upper component in the listing above)

Most propably I had done something wrong, but maybe, there is a glitch in this chapter. Could you please have a look?

Edit: I formerly pasted a piece of Code, where I bound the type impl FnOnce() -> i32 to the variable double count. This was a attempt to troubleshoot this issue. Sorry for that.

Thanks

Simon

@gbj
Copy link
Contributor

gbj commented Apr 9, 2025

This only works on nightly, where signals directly implement the Fn() traits. The book was later rewritten to default to stable-only features. This probably just needs to be reworded now to clarify that this only works on nightly.

@seemawn1981
Copy link
Author

ah, that explains a lot. Yeah, that would be great, as I prefer stable Rust, as long it is possible to drive Leptos with stable features.

Besides: Rust 2024 has been released shortly, so I thougt, that some of the nightly features are merged to stable right now, aren't they?

Thanks a lot!

@gbj gbj closed this as completed in 7e9ce9a Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants