Replies: 3 comments 1 reply
-
I'm a little unclear on what the version was that didn't compile, but I assume it was something like this: #[component]
fn Parent(cx: Scope, children: Children) -> impl IntoView {
let (value, set_value) = create_signal(cx, 0);
view! { cx,
<button on:click=move |_| set_value.update(|n| *n += 1)>"+1"</button>
{move || if value() % 2 == 0 { Some(children(cx)) } else { None }}
}
} What you want is fn Parent(cx: Scope, children: ChildrenFn) -> impl IntoView { It's given as an aside in the chapter on children so maybe it's easy to miss:
EDIT: For what it's worth, #[component]
fn Parent(cx: Scope, children: Children) -> impl IntoView {
let (value, set_value) = create_signal(cx, 0);
let children = children(cx);
view! { cx,
<button on:click=move |_| set_value.update(|n| *n += 1)>"+1"</button>
{move || if value() % 2 == 0 { Some(children.clone()) } else { None }}
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank very much
I should've stated that I'm using v0.3.0
using
Of course, but I could confirm that doing this is forcing children components to initialize regardless of the conditional rendering state. |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, some problems are dragging this discussion for longer. What I'm trying to achieve this time is passing "passed children" to a "final wrapper" and "through a conditional wrapping component" as described in the figures below.
use leptos::*;
fn main() {
console_error_panic_hook::set_once();
leptos::mount_to_body(move |cx| {
view! { cx, <App/> }
})
}
#[component]
fn App(cx: Scope) -> impl IntoView {
view! { cx,
<div style="border: solid 1px black; margin: 10px;">
"App"
<ConditionalWrapper>
<div style="border: solid 1px blue; margin: 10px;">"content sent through conditional wrapper"</div>
</ConditionalWrapper>
</div>
}
}
#[component]
fn ConditionalWrapper(cx: Scope, children: ChildrenFn) -> impl IntoView {
let (visible, set_visible) = create_signal(cx, true);
let interval = set_interval_with_handle(move || {
set_visible.update(|v| *v = !*v);
}, std::time::Duration::from_secs(1));
view! { cx,
<div style="border: solid 1px red; margin: 10px; min-height: 150px;">
{move || format!("Conditional Wrapper ({})", if visible.get() { "visible" } else { "invisible" } )}
// This works but:
// - using cx from ConditionalWrapper (not from Wrapper)
<Wrapper>
{children(cx)}
</Wrapper>
// Show won't accept {children(cx)},
// - due to `move` and conflicts between the view and a closure internal to `Show`
// - children is non clonable
// - please un-comment the wrapped 3 line to confirm
<Show when=move || visible.get()
fallback=|cx| view! { cx, }
>
// <Wrapper>
// {children(cx)}
// </Wrapper>
""
</Show>
</div>
}
}
#[component]
fn Wrapper(cx: Scope, children: ChildrenFn) -> impl IntoView {
view! { cx,
<div style="border: solid 1px green; margin: 10px;">
"Wrapper"
{children(cx)}
</div>
}
} Questions: |
Beta Was this translation helpful? Give feedback.
-
Conditional rendering for passed children is possible, but I'm receiving compile time errors after wrapping them in a closure for reactive conditional rendering.
Children
isn'tCopy
and deriving from a closure wrappingChildren(cx)
didn't help ( this closure implementsFnOnce
, notFn
). But after spending more time, I could finally get around this problem with the help from a wrapping signal.Questions:
Beta Was this translation helpful? Give feedback.
All reactions