Preferred way to build custom components with builder syntax #2640
-
Hi, I am interested in using Leptos and am currently leaning toward preferring the builder syntax because so far it seems to have better auto-complete support in RustRover/JetBrains. However, I haven't been able to find many examples of how to build a component (other than the built-in HTML methods ( 1. What is the preferred method to build a custom component, e.g. the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I guess I'm not sure whether you are mostly talking about using your own components, or using the built-in control flow components (Show, Suspense, etc.) For your own components, you can of course just write a function instead of using the component macro. The macro is mostly useful so we can have named arguments in the XML-like syntax. For example here's a perfectly reasonable way to have a component with children with no macros: #[component]
pub fn App() -> impl IntoView {
WithChildren(|| p().child("foo bar").into_view())
}
pub fn WithChildren(children: impl FnOnce() -> View) -> impl IntoView {
div().child(children())
} If you're talking about the built-in components that take the #[component]
pub fn App() -> impl IntoView {
(
WithChildren(Box::new(|| p().child("Foo").into_view().into())),
WithChildren(Box::new(|| {
[p().child("Bar").into_view(), p().child("Baz").into_view()]
.into_iter()
.collect()
})),
)
}
pub fn WithChildren(children: Children) -> impl IntoView {
div().child(children())
} |
Beta Was this translation helpful? Give feedback.
I guess I'm not sure whether you are mostly talking about using your own components, or using the built-in control flow components (Show, Suspense, etc.)
For your own components, you can of course just write a function instead of using the component macro. The macro is mostly useful so we can have named arguments in the XML-like syntax.
For example here's a perfectly reasonable way to have a component with children with no macros:
If you're talking about the built-in components that take …