leptos-rs / book

The home for the Leptos book, which can be found deployed at https://book.leptos.dev
MIT License
63 stars 59 forks source link

Bug in Sample Code in chapter "A Basic Component" #54

Open liangcorp opened 5 months ago

liangcorp commented 5 months ago

There is a bug in the first sample code in chapter "A Basic Component". set_count(3) should be set_count.set(3).

#[component]
fn App() -> impl IntoView {
    let (count, set_count) = create_signal(0);

    view! {
        <button
            on:click=move |_| {
                set_count(3);
            }
        >
            "Click me: "

            {move || count.get()}
        </button>
    }
}

Screenshot from 2024-01-29 01-17-57

gbj commented 5 months ago

Thanks for your message, and apologies for the confusion!

The README in the repo notes that most of the examples default to the nightly feature on Leptos, which enables this syntax. The "Getting Started" mentions this distinction too, but could maybe be clearer about the fact that the book is using the nightly syntax and if you're using stable, then the function-call syntax for signals shouldn't work.

We've discussed migrating the book to use the stable syntax by default in the future. I'm generally hesitant to do that because I find the mental-model advantages of the nightly syntax really useful:

// nightly: any signal access is a function call
let (count, set_count) = create_signal(1);
let double_count = move || count() * 2;
assert_eq!(count(), 1);
assert_eq!(double_count(), 1);

// stable: I have to remember whether something is a signal or derived, and update the syntax if that changes
let (count, set_count) = create_signal(1);
let double_count = move || count.get() * 2;

// works
assert_eq!(double_count(), 1);
// compile error
assert_eq!(count(), 1);
liangcorp commented 5 months ago

Understandable. I would suggest to use stable as documentation base after leptos reaches 1.0.