Struct leptos::Effect

source ·
pub struct Effect<T> { /* private fields */ }
Expand description

A handle to an effect, can be used to explicitly dispose of the effect.

Implementations§

source§

impl<T> Effect<T>
where T: 'static,

source

pub fn new(f: impl Fn(Option<T>) -> T + 'static) -> Effect<T>

Effects run a certain chunk of code whenever the signals they depend on change. create_effect immediately runs the given function once, tracks its dependence on any signal values read within it, and reruns the function whenever the value of a dependency changes.

Effects are intended to run side-effects of the system, not to synchronize state within the system. In other words: don’t write to signals within effects. (If you need to define a signal that depends on the value of other signals, use a derived signal or create_memo).

The effect function is called with an argument containing whatever value it returned the last time it ran. On the initial run, this is None.

By default, effects do not run on the server. This means you can call browser-specific APIs within the effect function without causing issues. If you need an effect to run on the server, use create_isomorphic_effect.

let a = RwSignal::new(0);
let b = RwSignal::new(0);

// ✅ use effects to interact between reactive state and the outside world
Effect::new(move |_| {
  // immediately prints "Value: 0" and subscribes to `a`
  log::debug!("Value: {}", a.get());
});

a.set(1);
// ✅ because it's subscribed to `a`, the effect reruns and prints "Value: 1"

// ❌ don't use effects to synchronize state within the reactive system
Effect::new(move |_| {
  // this technically works but can cause unnecessary re-renders
  // and easily lead to problems like infinite loops
  b.set(a.get() + 1);
});
source

pub fn new_isomorphic(f: impl Fn(Option<T>) -> T + 'static) -> Effect<T>

Creates an effect; unlike effects created by create_effect, isomorphic effects will run on the server as well as the client.

let a = RwSignal::new(0);
let b = RwSignal::new(0);

// ✅ use effects to interact between reactive state and the outside world
Effect::new_isomorphic(move |_| {
  // immediately prints "Value: 0" and subscribes to `a`
  log::debug!("Value: {}", a.get());
});

a.set(1);
// ✅ because it's subscribed to `a`, the effect reruns and prints "Value: 1"

// ❌ don't use effects to synchronize state within the reactive system
Effect::new_isomorphic(move |_| {
  // this technically works but can cause unnecessary re-renders
  // and easily lead to problems like infinite loops
  b.set(a.get() + 1);
});
source

pub fn with_value_mut<U>( &self, f: impl FnOnce(&mut Option<T>) -> U ) -> Option<U>

Applies the given closure to the most recent value of the effect.

Because effect functions can return values, each time an effect runs it consumes its previous value. This allows an effect to store additional state (like a DOM node, a timeout handle, or a type that implements Drop) and keep it alive across multiple runs.

This method allows access to the effect’s value outside the effect function. The next time a signal change causes the effect to run, it will receive the mutated value.

Trait Implementations§

source§

impl<T> Clone for Effect<T>
where T: Clone,

source§

fn clone(&self) -> Effect<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Effect<T>
where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<T> Default for Effect<T>
where T: Default,

source§

fn default() -> Effect<T>

Returns the “default value” for a type. Read more
source§

impl<T> From<Effect<T>> for Disposer

source§

fn from(effect: Effect<T>) -> Disposer

Converts to this type from the input type.
source§

impl<T> Hash for Effect<T>
where T: Hash,

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T> PartialEq for Effect<T>
where T: PartialEq,

source§

fn eq(&self, other: &Effect<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> SignalDispose for Effect<T>

source§

fn dispose(self)

Disposes of the signal. This: Read more
source§

impl<T> Copy for Effect<T>
where T: Copy,

source§

impl<T> Eq for Effect<T>
where T: Eq,

source§

impl<T> StructuralPartialEq for Effect<T>

Auto Trait Implementations§

§

impl<T> Freeze for Effect<T>

§

impl<T> RefUnwindSafe for Effect<T>
where T: RefUnwindSafe,

§

impl<T> Send for Effect<T>
where T: Send,

§

impl<T> Sync for Effect<T>
where T: Sync,

§

impl<T> Unpin for Effect<T>
where T: Unpin,

§

impl<T> UnwindSafe for Effect<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<El> ElementDescriptorBounds for El
where El: Debug,