Struct leptos::Scope

source ·
pub struct Scope { /* private fields */ }
Expand description

A Each scope can have child scopes, and may in turn have a parent.

Scopes manage memory within the reactive system. When a scope is disposed, its cleanup functions run and the signals, effects, memos, resources, and contexts associated with it no longer exist and should no longer be accessed.

You generally won’t need to create your own scopes when writing application code. However, they’re very useful for managing control flow within an application or library. For example, if you are writing a keyed list component, you will want to create a child scope for each row in the list so that you can dispose of its associated signals, etc. when it is removed from the list.

Every other function in this crate takes a Scope as its first argument. Since Scope is Copy and 'static this does not add much overhead or lifetime complexity.

Implementations§

The unique identifier for this scope.

Creates a child scope and runs the given function within it, returning a handle to dispose of it.

The child scope has its own lifetime and disposer, but will be disposed when the parent is disposed, if it has not been already.

This is useful for applications like a list or a router, which may want to create child scopes and dispose of them when they are no longer needed (e.g., a list item has been destroyed or the user has navigated away from the route.)

Creates a child scope and runs the given function within it, returning the function’s return type and a handle to dispose of it.

The child scope has its own lifetime and disposer, but will be disposed when the parent is disposed, if it has not been already.

This is useful for applications like a list or a router, which may want to create child scopes and dispose of them when they are no longer needed (e.g., a list item has been destroyed or the user has navigated away from the route.)

Suspends reactive tracking while running the given function.

This can be used to isolate parts of the reactive graph from one another.

let (a, set_a) = create_signal(cx, 0);
let (b, set_b) = create_signal(cx, 0);
let c = create_memo(cx, move |_| {
    // this memo will *only* update when `a` changes
    a() + cx.untrack(move || b())
});

assert_eq!(c(), 0);
set_a(1);
assert_eq!(c(), 1);
set_b(1);
// hasn't updated, because we untracked before reading b
assert_eq!(c(), 1);
set_a(2);
assert_eq!(c(), 3);

Disposes of this reactive scope.

This will

  1. dispose of all child Scopes
  2. run all cleanup functions defined for this scope by on_cleanup.
  3. dispose of all signals, effects, and resources owned by this Scope.

Returns the the parent Scope, if any.

Returns IDs for all Resources found on any scope.

Returns IDs for all Resources found on any scope that are pending from the server.

Returns IDs for all Resources found on any scope.

Registers the given SuspenseContext with the current scope, calling the resolver when its resources are all resolved.

The set of all HTML fragments currently pending. Returns a tuple of the hydration ID of the previous element, and a pinned Future that will yield the <Suspense/> HTML when all resources are resolved.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more