Skip to content
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

HtmlElement.attr() doesn't add actually add attr #1645

Closed
smessmer opened this issue Sep 3, 2023 · 2 comments
Closed

HtmlElement.attr() doesn't add actually add attr #1645

smessmer opened this issue Sep 3, 2023 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@smessmer
Copy link

smessmer commented Sep 3, 2023

Describe the bug

#[component]
pub fn Button(
    cx: Scope,
    children: Children.
) -> impl IntoView {
    let mut button = view! {cx,
        <button
            type="button"
        >
            {children(cx)}
        </button>
    };
    button = button.attr("data-my-attr", "value");
    button
}

I expected this to generate a <button> with a data-my-attr attribute, but in the actual HTML generated, the button does not have that attribute.

Use case: I want to use this to dynamically add some data- attributes. The exact attributes to set are defined by one of the props being passed in, but that Prop is a custom struct and the logic for which attributes to add to the button should live in that struct's module, not here.

Leptos Dependencies

Please copy and paste the Leptos dependencies and features from your Cargo.toml.

For example:

leptos = { version = "0.4", features = ["nightly"] }
leptos_axum = { version = "0.4", optional = true }
leptos_meta = { version = "0.4", features = ["nightly"] }
leptos_router = { version = "0.4", features = ["nightly"] }
@gbj
Copy link
Collaborator

gbj commented Sep 3, 2023

This is working as designed, granted that "don't mix the view macro and builder syntax in SSR" should be better documented.

For the reason this doesn't work, see here for a description of the SSR optimizations.

The solution is simply to use only the builder syntax:

button()
  .attr("type", "button")
  .attr("data-foo", "bar")

@smessmer
Copy link
Author

smessmer commented Sep 4, 2023

Oh interesting, I thought the view! macro was just expanding to code using the builder pattern since it does return a type usable in the builder pattern.

If they are really incompatible, I think this should at least throw a run time panic. Better a build time error. Being silent but doing the wrong thing isn't great.

@gbj gbj added the documentation Improvements or additions to documentation label Sep 4, 2023
@gbj gbj closed this as completed in a07984b Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants