Trait sycamore::generic_node::GenericNode[][src]

pub trait GenericNode: Debug + Clone + PartialEq + Eq + Hash + 'static {
    type EventType;

    const USE_HYDRATION_CONTEXT: bool;
    const CLIENT_SIDE_HYDRATION: bool;
Show 24 methods fn element(tag: &str) -> Self;
fn text_node(text: &str) -> Self;
fn marker_with_text(text: &str) -> Self;
fn set_attribute(&self, name: &str, value: &str);
fn remove_attribute(&self, name: &str);
fn set_class_name(&self, value: &str);
fn add_class(&self, class: &str);
fn remove_class(&self, class: &str);
fn set_property(&self, name: &str, value: &JsValue);
fn remove_property(&self, name: &str);
fn append_child(&self, child: &Self);
fn first_child(&self) -> Option<Self>;
fn insert_child_before(
        &self,
        new_node: &Self,
        reference_node: Option<&Self>
    );
fn remove_child(&self, child: &Self);
fn replace_child(&self, old: &Self, new: &Self);
fn insert_sibling_before(&self, child: &Self);
fn parent_node(&self) -> Option<Self>;
fn next_sibling(&self) -> Option<Self>;
fn remove_self(&self);
fn event(&self, name: &str, handler: Box<dyn Fn(Self::EventType)>);
fn update_inner_text(&self, text: &str);
fn dangerously_set_inner_html(&self, html: &str);
fn clone_node(&self) -> Self; fn marker() -> Self { ... }
}
Expand description

Abstraction over a rendering backend.

You would probably use this trait as a trait bound when you want to accept any rendering backend. For example, components are often generic over GenericNode to be able to render to different backends.

Note that components are NOT represented by GenericNode. Instead, components are disappearing, meaning that they are simply functions that generate GenericNodes inside a new reactive context. This means that using components add minimal overhead.

Sycamore ships with 2 rendering backends out of the box:

  • DomNode - Rendering in the browser (to real DOM nodes).
  • SsrNode - Render to a static string (often on the server side for Server Side Rendering, aka. SSR).

To implement your own rendering backend, you will need to create a new struct which implements GenericNode.

Cloning

GenericNodes should be cheaply cloneable (usually backed by a Rc or other reference counted container) and preserve reference equality.

Associated Types

The type of the event that is passed to the event handler.

Associated Constants

Whether this rendering backend needs the hydration registry.

Required methods

Create a new element node.

Create a new text node.

Create a marker (dummy) node with text content. For empty marker, prefer GenericNode::marker instead.

Sets an attribute on a node.

Removes an attribute on a node.

Sets the class attribute on a node. This should have the same outcome as calling set_attribute("class", value). For DomNode, this sets the className property directly which is about 2x faster (on Chrome).

Sets a property on a node.

Removes a property on a node.

Appends a child to the node’s children.

Get the first child of the node.

Insert a new child node to this node’s children. If reference_node is Some, the child will be inserted before the reference node. Else if None, the child will be inserted at the end.

Remove a child node from this node’s children.

Replace a child node from this node’s children with a new child node.

Insert a new node before this node.

Returns the parent node, or None if detached.

Returns the next sibling, or None if this node is the last sibling.

Remove this node from the tree.

Add a [EventHandler] to the event name.

Update inner text of the node. If the node has elements, all the elements are replaced with a new text node.

Updates the inner html of the node. The html will not be parsed in non-browser environments. This means that accessing methods such as first_child will return None.

Create a deep clone of the node.

Provided methods

Create a marker (dummy) node. For DomNode, this is implemented by creating an empty comment node. This is used, for example, in Keyed and Indexed for scenarios where you want to push a new item to the end of the list. If the list is empty, a dummy node is needed to store the position of the component.

Implementors