How to Drill Props #3744
Answered
by
ranile
Android789515
asked this question in
Q&A
How to Drill Props
#3744
-
I'd like to drill a Units prop down: App (Units Created) ----> Sizes (Units) ----> Hexagon At the moment I am cloning the props to drill: Sizes Component#[derive(Properties, PartialEq)]
pub struct Props {
pub units: Units,
}
pub struct Sizes;
impl Component for Sizes {
type Message = ();
type Properties = Props;
fn create(_: &yew::Context<Self>) -> Self {
Self
}
fn view(&self, context: &yew::Context<Self>) -> Html {
let hexagons = {
if context.props().units == Units::SAE {
[ "1/8", ]
} else {
[ "3", ]
}
};
html! {
<ul class={classes!(self.styles())}>
{hexagons.into_iter()
.enumerate()
.map(|(key, unit_value)| {
html! {
<Hexagon
key={key}
size={key}
units={context.props().units.clone()}
text={unit_value}
/>
}
})
.collect::<Html>()
}
</ul>
}
}
} Hexagon Component#[derive(Properties, PartialEq)]
pub struct Props {
pub size: usize,
pub units: Units,
pub text: &'static str,
}
pub struct Hexagon;
impl Component for Hexagon {
type Message = ();
type Properties = Props;
fn create(_: &yew::Context<Self>) -> Self {
Self
}
fn view(&self, context: &yew::Context<Self>) -> Html {
html! {
<div class={classes!(
self.styles(),
)}>
<div class={classes!(
"hexagon"
)}>
{context.props().text}
</div>
</div>
}
}
} I don't know if this is the correct way to do this. |
Beta Was this translation helpful? Give feedback.
Answered by
ranile
Oct 6, 2024
Replies: 1 comment
-
Cloning is the perfectly valid way to pass props down. You want want to make sure the Units type is reference counted so it the data itself isn't cloned every time. The same recommendations about props apply here as well: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Android789515
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cloning is the perfectly valid way to pass props down. You want want to make sure the Units type is reference counted so it the data itself isn't cloned every time. The same recommendations about props apply here as well:
AttrValue
instead ofString
, etc