-
Hi, I have a single For this purpose, I explored three approaches, each with its own issues: 1.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Option 1 works exactly as expected (or rather, doesn't work exactly as expected): the Option 2 is also doing what it should do, although it's not what you're hoping it will do: you are reading reactively from <Show when=move || state.selected_session().get().is_some()>
// add a log here
{leptos::logging::log!("running this again!")}
<Content
// this reactive read is tracked inside the Show, rather than being untracked
session={state.selected_session().unwrap().get()}
/>
</Show> However, if you don't do that // ...
<Show when=move || state.selected_session().get().is_some()>
<Content session={state.selected_session().unwrap()} />
</Show>
}
}
#[component]
pub fn Content(#[prop(into)] session: Field<Field<Session>>) -> impl IntoView {
leptos::logging::log!("Hey!");
view! {
{move || {
// add .get() here
if session.get().title().with(String::len) == 7 {
Either::Right(view! { "Title has seven letters" })
} else {
Either::Left(view! { "Title doesn't have seven letters" })
}
}}
}
} Option 3 This is what I tend to use.
Presumably you could/would just define a method for this? (You can use a newtype or an extension trait to add a method to
Fair enough. |
Beta Was this translation helpful? Give feedback.
-
Hi @gbj, I think I found an inconsistency in the second option you suggested. |
Beta Was this translation helpful? Give feedback.
Option 1 works exactly as expected (or rather, doesn't work exactly as expected): the
Session
you store is a deep clone of the values at the moment that you store it, but there doesn't (and can't, and shouldn't) exist any connection between the original and the clone.Option 2 is also doing what it should do, although it's not what you're hoping it will do: you are reading reactively from
state.selected_session
inside the effect controlling theShow
, not inside theContent
, so it does rerender theContent
again. This is what would happen with any component and any signal being read in this way and passed to a component, rather than passed to the component as a signal. To put it another wa…