Skip to content

Can I store view in a variable that I would wrap? #1644

Answered by gbj
Umenokin asked this question in Q&A
Discussion options

You must be logged in to vote

It would be helpful to see an example of your actual code that doesn't compile, and to know what the compile errors are. What you're trying to do is perfectly achievable; here would be one example that does compile:

#[component]
pub fn Logo(#[prop(optional)] href: Option<&'static str>) -> impl IntoView {
    let content = view! { <p>"ok"</p> };

    move || match href {
        Some(href) => {
            let content = content.clone();
            view! {
                <A href={href}>{content}</A>
            };
        }
        .into_view(),
        None => content.clone().into_view(),
    }
}

Note, by the way, that several of your view! invocations are unnecessary: the move || match

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Umenokin
Comment options

Answer selected by Umenokin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants