github leptos-rs/leptos v0.4.8

latest releases: v0.7.0-preview2, v0.6.11, v0.6.10...
9 months ago

There was a log erroneously left in 0.4.7, so I rereleased as 0.4.8. Notes for 0.4.7 below.

This is mostly a bug-fix release with a few small features. There will probably be one or two more patch releases before 0.5.0.

New Features

mut in component props

This didn't work before; now it does:

#[component]
fn TestMutCallback<'a, F>(
    cx: Scope,
    mut callback: F,
    value: &'a str,
) -> impl IntoView
where
    F: FnMut(u32) + 'static,
{

MaybeProp type

MaybeProp is essentially a wrapper for Option<MaybeSignal<Option<T>>> with a nicer API. This is useful for something like a component library, where you want to give a user the maximum flexibility in the types they give to props while still checking that they're the appropriate T.

This allows you to define optional props that

  1. may or may not be provided
  2. if provided, may be a plain value or a signal
  3. if a signal type, may or may not have a value (i.e., are a Signal<Option<T>>)

Implementing From<_> for a plain Signal<T> type here is possible, but depends on the changes in 0.5.0, so I'll add it for that release.

#[component]
pub fn App(cx: Scope) -> impl IntoView {
    let (count, set_count) = create_signal(cx, 5);

    view! { cx,
        <TakesMaybeProp/>
        <TakesMaybeProp maybe=42/>
        <TakesMaybeProp maybe=Signal::derive(cx, move || Some(count.get()))/>
    }
}

#[component]
pub fn TakesMaybeProp(cx: Scope, #[prop(optional, into)] maybe: MaybeProp<i32>) -> impl IntoView {
    // .get() gives you `Option<T>`, replacing `self.as_ref().and_then(|n| n.get())`
    let value: Option<i32> = maybe.get();
    // .with() takes a plain `&T`; returns `None` if the prop isn't present or the inner value is None
    let plus_one = maybe.with(|n: &i32| n + 1);

    view! { cx,
        <p>{maybe}</p>
    }
}

Don't miss a new leptos release

NewReleases is sending notifications on new releases.