Skip to main content

Effect

Struct Effect 

Source
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

Source

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()
Source

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()
Source

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()
Source

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()
Source

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()))
Source

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§

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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