Replies: 4 comments 2 replies
-
Hi — I had missed that post but theirs was actually about rendering different components dynamically, not different elements. Because we have typed elements we don't have quite this level of flexibility, but there are two options
if some_condition {
view! { cx, <div class="title">{&text}</div> }.into_any()
} else {
view! { cx, <p class="title">{&text}</p> }.into_any()
}
let el = if level == 1 {
h1(cx).into_any()
} else if level == 2 {
h2(cx).into_any()
};
el.class("title").child(text) |
Beta Was this translation helpful? Give feedback.
-
If you want to take any element as a prop, then you should take the type use leptos::html::AnyElement;
#[component]
fn TakesAny(cx: Scope, element: HtmlElement<AnyElement>) -> impl IntoView {
view! { cx,
<div>
{element}
</div>
}
} used as view! { cx,
<div>
<TakesAny element=view! { cx, <p>"Test"</p>}.into_any()/>
</div>
} |
Beta Was this translation helpful? Give feedback.
-
What if I want to choose between element and component like router "A" component or custom one? Like: if some_condition {
view! { cx, <button>{inner}</button> }.into_any()
} else {
view! { cx, <A>{inner}</A> } // has no function into_any...
} |
Beta Was this translation helpful? Give feedback.
-
See https://leptos-rs.github.io/leptos/view/06_control_flow.html#note-type-conversions |
Beta Was this translation helpful? Give feedback.
-
Yew uses
html! { <@{format!("h{}", level)} class="title">{ text }</@> }
to render dynamic tag names.Is there a way in leptos to achieve the same?
Oh, just realised there's another discussions thread asking the same question: #438
Beta Was this translation helpful? Give feedback.
All reactions