#![allow(non_snake_case, unused)]
use leptos::prelude::*;
// Often, you want to pass some kind of child view to another
// component. There are two basic patterns for doing this:
// - "render props": creating a component prop that takes a function
// that creates a view
// - the `children` prop: a special property that contains content
// passed as the children of a component in your view, not as a
// property
#[component]
pub fn App() -> impl IntoView {
let (items, set_items) = signal(vec![0, 1, 2]);
let render_prop = move || {
let len = move || items.read().len();
view! {
"Length: " {len}
}
};
view! {
// This component just displays the two kinds of children,
// embedding them in some other markup
`render_prop`
// (this doesn't work for HTML element attributes)
render_prop
>
// these look just like the children of an HTML element
"Here's a child."
"Here's another child."
// This component actually iterates over and wraps the children
"Here's a child."
"Here's another child."
}
}
/// Displays a `render_prop` and some children within markup.
#[component]
pub fn TakesChildren(
/// Takes a function (type F) that returns anything that can be
/// converted into a View (type IV)
render_prop: F,
/// `children` takes the `Children` type
/// this is an alias for `Box Fragment>`
/// ... aren't you glad we named it `Children` instead?
children: Children,
) -> impl IntoView
where
F: Fn() -> IV,
IV: IntoView,
{
view! {
""
"Render Prop"
{render_prop()}
"Children"
{children()}
}
}
/// Wraps each child in an `` and embeds them in a ``.
#[component]
pub fn WrapsChildren(children: ChildrenFragment) -> impl IntoView {
// children() returns a `Fragment`, which has a
// `nodes` field that contains a Vec
// this means we can iterate over the children
// to create something new!
let children = children()
.nodes
.into_iter()
.map(|child| view! { - {child}
})
.collect::>();
view! {
""
// wrap our wrapped children in a UL
}
}
fn main() {
leptos::mount::mount_to_body(App)
}