Open milesj opened 5 days ago
props
needs to become a mutable reference, and the lifetime of the returned element becomes 'a
:#[component]
pub fn Entry<'a>(props: &mut EntryProps<'a>) -> impl Into<AnyElement<'a>> {
element! {
Box {
Box(margin_right: 1) {
Text(content: format!("{}:", props.title))
}
Box {
#(&mut props.children)
}
}
}
}
element!
macro like so:#[derive(Default, Props)]
pub struct ExampleProps<'a> {
pub my_child: Option<AnyElement<'a>>,
}
#[component]
fn Example<'a>(props: &mut ExampleProps<'a>) -> impl Into<AnyElement<'a>> {
element! {
Box {
#(&mut props.my_child)
}
}
}
fn main() {
element! {
Example(my_child: element! {
Text(content: "foo")
}.into_any())
}
.print();
}
Or alternatively, if you want to avoid nesting, you can define the child beforehand:
fn main() {
let my_child = element! {
Text(content: "foo")
}
.into_any();
element! {
Example(my_child)
}
.print();
}
Awesome, got these working, thank you! It would be great of their was an example file with all the different ways things can be done in regards to the syntax.
@ccbrown Just have a few questions so far that I haven't been able to figure out.
1 - How to pass children through for custom components? Maybe we need a
Fragment
component here?2 - Is it possible to render components as props to other components? Or is it only possible with children?