Trait leptos::SignalGet

source ·
pub trait SignalGet {
    type Value;

    // Required methods
    fn get(&self) -> Self::Value;
    fn try_get(&self) -> Option<Self::Value>;
}
Expand description

This trait allows getting an owned value of the signals inner type.

Required Associated Types§

source

type Value

The value held by the signal.

Required Methods§

source

fn get(&self) -> Self::Value

Clones and returns the current value of the signal, and subscribes the running effect to this signal.

§Panics

Panics if you try to access a signal that is owned by a reactive node that has been disposed.

source

fn try_get(&self) -> Option<Self::Value>

Clones and returns the signal value, returning Some if the signal is still alive, and None otherwise.

Implementors§

source§

impl SignalGet for Trigger

§

type Value = ()

source§

impl<S, T> SignalGet for Resource<S, T>
where S: Clone, T: Clone,

§

type Value = Option<T>

source§

impl<T> SignalGet for MaybeSignal<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 2);
let static_value: MaybeSignal<i32> = 5.into();

// this function takes any kind of wrapped signal
fn above_3(arg: &MaybeSignal<i32>) -> bool {
    arg.get() > 3
}

assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
assert_eq!(above_3(&static_value.into()), true);
§

type Value = T

source§

impl<T> SignalGet for MaybeProp<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(Some(2));
let double = |n| n * 2;
let double_count = MaybeProp::derive(move || count.get().map(double));
let memoized_double_count = create_memo(move |_| count.get().map(double));
let static_value = 5;

// this function takes either a reactive or non-reactive value
fn above_3(arg: &MaybeProp<i32>) -> bool {
    // ✅ calling the signal clones and returns the value
    //    it is a shorthand for arg.get()q
    arg.get().map(|arg| arg > 3).unwrap_or(false)
}

assert_eq!(above_3(&None::<i32>.into()), false);
assert_eq!(above_3(&static_value.into()), true);
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
§

type Value = Option<T>

source§

impl<T> SignalGet for Memo<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(0);
let double_count = create_memo(move |_| count.get() * 2);

assert_eq!(double_count.get(), 0);
set_count.set(1);

// can be `double_count()` on nightly
// assert_eq!(double_count(), 2);
assert_eq!(double_count.get(), 2);
§

type Value = T

source§

impl<T> SignalGet for ReadSignal<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(0);

assert_eq!(count.get(), 0);

// count() is shorthand for count.get() on `nightly`
// assert_eq!(count.get(), 0);
§

type Value = T

source§

impl<T> SignalGet for RwSignal<T>
where T: Clone,

§Examples

let count = create_rw_signal(0);

assert_eq!(count.get(), 0);

// count() is shorthand for count.get() on `nightly`
// assert_eq!(count(), 0);
§

type Value = T

source§

impl<T> SignalGet for Signal<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(2);
let double_count = Signal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 2);

// this function takes any kind of wrapped signal
fn above_3(arg: &Signal<i32>) -> bool {
    arg.get() > 3
}

assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
§

type Value = T