Rendering in Dioxus involves creating and updating the virtual DOM (VDOM) and then converting it into a format suitable for the target platform (e.g., web, desktop, server-side rendering). Here's an overview of how rendering works based on the provided context:
1. Virtual DOM (VDOM) Mechanism
At its core, Dioxus uses a Virtual DOM to manage UI updates efficiently. The Virtual DOM system allows you to define components and their state declaratively, which Dioxus then reconciles and renders.
Components are functions that take properties (props) and return an Element, which is a Result<VNode, RenderError>. These components are defined using the rsx! macro, which simplifies building the virtual DOM structure.
The VirtualDom struct in Dioxus is responsible for managing the lifecycle of components, tracking dirty (modified) scopes, and reconciling the UI tree.
The key steps in the virtual DOM process include:
Creation: The VirtualDom is initialized with a root component using VirtualDom::new().
Rebuilding: The rebuild() function performs a full re-render of the virtual DOM and computes the necessary mutations to update the real DOM.
Diffing: The render_immediate() function computes the differences (diffs) between the current and previous VDOM states to generate optimal updates.
2. Reactive Rendering
Dioxus employs a reactive system to determine when components or parts of the virtual DOM need to be re-rendered.
Reactive Contexts: Components, hooks (like use_effect, use_memo), and resources have reactive contexts that track dependencies on reactive values (like signals or state).
Tracked Values: Signals (use_signal) and other reactive values trigger updates when their state changes.
The scheduler ensures that:
Dirty scopes (components that need re-rendering) are processed in the correct order.
Effects run only after DOM updates are applied.
Observing state is always consistent, ensuring derived values like use_memo are accurate.
3. Rendering Pipeline
The rendering process can be broken into several steps:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The virtual DOM starts by running the components, which return Element objects. These elements describe the desired UI structure in terms of nodes (VNode), attributes, and children.
Step 2: Diffing
The Virtual DOM computes the differences (diffs) between the current and previous states:
Dirty scopes are re-run using the reactive system.
Changes are captured as Mutations, which describe how the real DOM or platform-specific tree should be updated.
Step 3: Writing Mutations
The render_immediate() function writes the computed mutations to a WriteMutations object. These mutations include operations like:
Creating new elements.
Updating attributes.
Removing elements.
If you're testing, you can use render_immediate_to_vec() to collect mutations into a vector.
Step 4: Applying Mutations
Once mutations are computed, they are applied by the renderer. For example:
In the web platform, mutations would update the browser's DOM.
For SSR (Server-Side Rendering), mutations are converted into an HTML string.
4. Server-Side Rendering (SSR)
Dioxus supports server-side rendering (SSR) through the Renderer struct:
The Renderer caches templates for efficient rendering.
It processes dynamic nodes and attributes by merging static templates with runtime data (DynamicNode).
The output is serialized HTML, which can also include hydration markers for client-side rehydration.
For example, the Renderer::render_to() function renders a VirtualDom into a buffer (e.g., a string) by traversing templates, dynamic nodes, and pre-rendered content.
5. Example Rendering Workflow
Here's an example workflow for rendering in Dioxus:
Initialization:
[Redacted
let mut vdom = VirtualDom::new(app);
vdom.rebuild(); // Fully initialize the virtual DOM.
Processing Work:
Use wait_for_work() to process events, tasks, and effects.
let mut renderer = Renderer::new();
let html = renderer.render(&vdom); // Render as an HTML string.
6. Template and Dynamic Node Handling
Dioxus optimizes rendering by caching templates and handling dynamic nodes efficiently:
Templates: Static parts of the UI are pre-rendered and cached, reducing runtime overhead.
Dynamic Nodes: Represent dynamic content (e.g., text, components, fragments), which are rendered separately during runtime.
Key Features in Rendering
Reactivity: Automatically track dependencies using signals and hooks.
Scheduler: Prioritizes dirty scopes, tasks, and effects for efficient updates.
SSR: Supports server-side rendering with template caching and hydration markers.
Cross-Platform: Rendering logic adapts to the target platform (web, desktop, SSR, etc.).
Example: Hello World App
The following example demonstrates a simple rendering pipeline:
[Redacted
use dioxus::prelude::*;
fn app() -> Element {
rsx! {
div { "Hello, world!" }
}
}
fn main() {
dioxus::launch(app);
}
The rsx! macro creates a VNode representing the div.
The VirtualDom renders the component tree and computes updates.
On the web, the renderer updates the browser DOM with these changes.
Overall, Dioxus combines a powerful virtual DOM, reactivity, and efficient diffing to deliver a performant and declarative rendering system suitable for a variety of platforms.