Skip to content

Is there a way to let raw attributes like class or id to be automatically applied to root node of component ? #399

Answered by gbj
leo91000 asked this question in Q&A
Discussion options

You must be logged in to vote

The short answer: No, you can't pass HTML element attributes directly to components are component props. They're conceptually different things: think of the component props as function arguments. The compiler is helpfully telling you that there's no class prop on MyComponent, which is true. You can add one and pass it through to the HTML element.

#[component]
fn MyComponent<'a>(cx: Scope, class: &'a str) -> impl IntoView {
    view! { cx
         <div class=class>Foo</div>
    }
}

or use a String or whatever type you'd like, if you don't want to deal with the lifetime annotation

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by leo91000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants