pub struct Effect;Expand description
Creates side effect callback that is rerun when reactive values read on it, change.
The usual way is to use use_side_effect, but you can use the several Effect methods like Effect::create inside use_hook
or Effect::create_global inside LaunchConfig::with_task.
Implementations§
Source§impl Effect
impl Effect
Sourcepub fn create(callback: impl FnMut() + 'static)
pub fn create(callback: impl FnMut() + 'static)
Runs the callback asynchronously in the current component. Reruns when a reactive value read by the callback changes.
Unlike Effect::create_sync, the first run is not immediate.
let count = use_state(|| 0);
use_hook(move || {
Effect::create(move || {
println!("Count: {}", *count.read());
})
});
rect()Sourcepub fn create_sync_with_gen(callback: impl FnMut(usize) + 'static)
pub fn create_sync_with_gen(callback: impl FnMut(usize) + 'static)
Runs immediately with generation 0.
Then, on every change the callback reruns with an increased generation.
let count = use_state(|| 0);
use_hook(move || {
Effect::create_sync_with_gen(move |generation| {
println!("Run {generation}: {}", *count.read());
})
});
rect()Sourcepub fn create_sync(callback: impl FnMut() + 'static)
pub fn create_sync(callback: impl FnMut() + 'static)
Runs immediately and then on every change the callback reruns with an increased generation.
let count = use_state(|| 0);
use_hook(move || {
Effect::create_sync(move || {
println!("Count: {}", *count.read());
})
});
rect()Sourcepub fn create_after(callback: impl FnMut() + 'static)
pub fn create_after(callback: impl FnMut() + 'static)
Spawns each callback run in a separate task, including the first run. Reruns when a reactive value read by the callback changes.
Unlike Effect::create, the callback does not run in the task
listening for changes.
let count = use_state(|| 0);
use_hook(move || {
Effect::create_after(move || {
println!("Count: {}", *count.read());
})
});
rect()Sourcepub fn create_value<T: 'static>(
callback: impl FnMut() -> T + 'static,
) -> State<T>
pub fn create_value<T: 'static>( callback: impl FnMut() -> T + 'static, ) -> State<T>
Computes the initial value immediately and returns it as State<T>.
Reactive changes rerun the callback in a task and update the value.
let count = use_state(|| 1);
let doubled = use_hook(move || Effect::create_value(move || *count.read() * 2));
rect().child(format!("Doubled: {}", *doubled.read()))Sourcepub fn create_global(callback: impl FnMut() + 'static) -> TaskHandle
pub fn create_global(callback: impl FnMut() + 'static) -> TaskHandle
Runs asynchronously and reruns when reactive values read by the callback change. Lives independently of components and windows.
Stops when the application exits or the returned TaskHandle is cancelled.
You should usually call this inside LaunchConfig::with_task, not in components.
let count = State::create_global(0);
launch(
LaunchConfig::new()
.with_exit_on_close(false)
.with_task(move |_| async move {
Effect::create_global(move || {
println!("Count: {}", *count.read());
});
}),
);Auto Trait Implementations§
impl Forget for Effect
impl Freeze for Effect
impl RefUnwindSafe for Effect
impl Send for Effect
impl Sync for Effect
impl Unpin for Effect
impl UnsafeUnpin for Effect
impl UnwindSafe for Effect
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<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