pub struct ScopeState { /* private fields */ }
Expand description

A component’s state separate from its props.

This struct exists to provide a common interface for all scopes without relying on generics.

Implementations§

Get the name of this component

Get the current render since the inception of this component

This can be used as a helpful diagnostic when debugging hooks/renders, etc

Get a handle to the currently active bump arena for this Scope

This is a bump memory allocator. Be careful using this directly since the contents will be wiped on the next render. It’s easy to leak memory here since the drop implementation will not be called for any objects allocated in this arena.

If you need to allocate items that need to be dropped, use bumpalo’s box.

Get a handle to the currently active head node arena for this Scope

This is useful for traversing the tree outside of the VirtualDom, such as in a custom renderer or in SSR.

Panics if the tree has not been built yet.

Try to get a handle to the currently active head node arena for this Scope

This is useful for traversing the tree outside of the VirtualDom, such as in a custom renderer or in SSR.

Returns None if the tree has not been built yet.

Get the height of this Scope - IE the number of scopes above it.

A Scope with a height of 0 is the root scope - there are no other scopes above it.

Example
let mut dom = VirtualDom::new(|cx|  cx.render(rsx!{ div {} }));
dom.rebuild();

let base = dom.base_scope();

assert_eq!(base.height(), 0);

Get the Parent of this Scope within this Dioxus crate::VirtualDom.

This ID is not unique across Dioxus crate::VirtualDoms or across time. IDs will be reused when components are unmounted.

The base component will not have a parent, and will return None.

Example
let mut dom = VirtualDom::new(|cx|  cx.render(rsx!{ div {} }));
dom.rebuild();

let base = dom.base_scope();

assert_eq!(base.parent(), None);

Get the ID of this Scope within this Dioxus crate::VirtualDom.

This ID is not unique across Dioxus crate::VirtualDoms or across time. IDs will be reused when components are unmounted.

Example
let mut dom = VirtualDom::new(|cx|  cx.render(rsx!{ div {} }));
dom.rebuild();
let base = dom.base_scope();

assert_eq!(base.scope_id(), 0);

Create a subscription that schedules a future render for the reference component

Notice: you should prefer using Self::schedule_update_any and Self::scope_id

Schedule an update for any component given its ScopeId.

A component’s ScopeId can be obtained from use_hook or the ScopeState::scope_id method.

This method should be used when you want to schedule an update for a component

Mark this scope as dirty, and schedule a render for it.

Get the ScopeId of a mounted component.

ScopeId is not unique for the lifetime of the crate::VirtualDom - a ScopeId will be reused if a component is unmounted.

Return any context of type T if it exists on this scope

Try to retrieve a shared state with type T from any parent scope.

Clones the state if it exists.

Expose state to children further down the crate::VirtualDom Tree. Does not require clone on the context, though we do recommend it.

This is a “fundamental” operation and should only be called during initialization of a hook.

For a hook that provides the same functionality, use use_provide_context and use_context instead.

If a state is provided that already exists, the new value will not be inserted. Instead, this method will return the existing value. This behavior is chosen so shared values do not need to be Clone. This particular behavior might change in the future.

Example
struct SharedState(&'static str);

static App: Component = |cx| {
    cx.use_hook(|| cx.provide_context(SharedState("world")));
    render!(Child {})
}

static Child: Component = |cx| {
    let state = cx.consume_state::<SharedState>();
    render!(div { "hello {state.0}" })
}

Pushes the future onto the poll queue to be polled after the component renders.

Spawns the future but does not return the TaskId

Spawn a future that Dioxus won’t clean up when this component is unmounted

This is good for tasks that need to be run after the component has been dropped.

Informs the scheduler that this task is no longer needed and should be removed.

This drops the task immediately.

Take a lazy crate::VNode structure and actually build it with the context of the efficient bumpalo::Bump allocator.

Example
fn Component(cx: Scope<Props>) -> Element {
    // Lazy assemble the VNode tree
    let lazy_nodes = rsx!("hello world");

    // Actually build the tree and allocate it
    cx.render(lazy_tree)
}

Create a dynamic text node using Arguments and the ScopeState’s internal Bump allocator

Allocate some text inside the ScopeState from Arguments

Uses the currently active Bump allocator

Convert any item that implements IntoDynNode into a DynamicNode using the internal Bump allocator

Create a new Attribute from a name, value, namespace, and volatile bool

“Volatile” referes to whether or not Dioxus should always override the value. This helps prevent the UI in some renderers stay in sync with the VirtualDom’s understanding of the world

Create a new DynamicNode::Component variant

The given component can be any of four signatures. Remember that an Element is really a Result<VNode>.

// Without explicit props
fn(Scope) -> Element;
async fn(Scope<'_>) -> Element;

// With explicit props
fn(Scope<Props>) -> Element;
async fn(Scope<Props<'_>>) -> Element;

Create a new EventHandler from an FnMut

Create a new AttributeValue with the listener variant from a callback

The callback must be confined to the lifetime of the ScopeState

Create a new AttributeValue with a value that implements [AnyValue]

Inject an error into the nearest error boundary and quit rendering

The error doesn’t need to implement Error or any specific traits since the boundary itself will downcast the error into a trait object.

Store a value between renders. The foundational hook for all other hooks.

Accepts an initializer closure, which is run on the first use of the hook (typically the initial render). The return value of this closure is stored for the lifetime of the component, and a mutable reference to it is provided on every render as the return value of use_hook.

When the component is unmounted (removed from the UI), the value is dropped. This means you can return a custom type and provide cleanup code by implementing the Drop trait

Example
use dioxus_core::ScopeState;

// prints a greeting on the initial render
pub fn use_hello_world(cx: &ScopeState) {
    cx.use_hook(|| println!("Hello, world!"));
}

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.

Calls U::from(self).

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

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.