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,
impl<T> Effect<T>where
T: 'static,
sourcepub fn new(f: impl Fn(Option<T>) -> T + 'static) -> Effect<T>
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);
});
sourcepub fn new_isomorphic(f: impl Fn(Option<T>) -> T + 'static) -> Effect<T>
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);
});
sourcepub fn with_value_mut<U>(
&self,
f: impl FnOnce(&mut Option<T>) -> U
) -> Option<U>
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> PartialEq for Effect<T>where
T: PartialEq,
impl<T> PartialEq for Effect<T>where
T: PartialEq,
source§impl<T> SignalDispose for Effect<T>
impl<T> SignalDispose for Effect<T>
impl<T> Copy for Effect<T>where
T: Copy,
impl<T> Eq for Effect<T>where
T: Eq,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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