Sycamore Documentation

repository·main·Indexed 25 days ago

https://github.com/sycamore-rs/sycamore

Sycamore is a reactive Rust library for building high-performance web applications via WebAssembly. It utilizes fine-grained reactivity instead of a virtual DOM to provide an ergonomic developer experience. Key features include the view! macro for components, a dedicated router for SPAs, Server-Side Rendering (SSR) support, and attribute passthrough for custom components.

Tokens
34.9K
Snippets
130
Records
252
Agent score
85%

What's inside Sycamore

  1. Overview of Sycamore

    main
    Sycamore is a reactive library for creating web applications using Rust and WebAssembly. It uses fine-grained reactivity instead of a virtual DOM, allowing for high performance and an ergonomic developer experience without requiring JavaScript.
  2. Explore Sycamore example patterns

    main

    Sycamore provides various examples demonstrating different features and patterns. Key examples include:

    • Core UI & Logic: counter (simple state), iteration (looping over data), components (UI abstraction), and higher-order-components (functions that create components).
    • Data & Async: http-request and http-request-builder (Suspense + async components for HTTP), timer (using futures), and transitions (Suspense + async transitions).
    • Routing & Navigation: router (demonstrating sycamore-router).
    • Rendering Strategies: ssr (Server-Side Rendering), ssr-suspense (SSR with suspense), ssr-streaming (SSR with streaming), and hydrate (making existing HTML reactive).
    • Advanced UI: motion (animation frames and tweened signals), svg (creating SVGs with the view! macro), and attributes-passthrough (dynamic attributes).
  3. Interpolate values in views

    main

    You can interpolate any value that implements Into<View>, such as strings, numbers, signals, or other views. When using complex expressions or signals, the view! macro automatically wraps them in a closure to create a dynamic view.

    let value = 123;
    let signal = create_signal(456);
    
    view! {
        p {
            "Value: " (value)
        }
        p {
            // Automatically becomes a dynamic view
            (signal.get() + 1)
        }
    }
  4. Run WASM tests with wasm-pack

    main

    To run WASM-specific tests, ensure wasm-pack is installed. You can run tests in a visible browser (like Chrome) or in headless mode.

    Chrome tests:

    cd packages/sycamore
    wasm-pack test --chrome --all-features

    Headless tests: Add the --headless flag to the command above.

    cd packages/sycamore
    wasm-pack test --chrome --all-features
  5. Handle events in views

    main

    Use the on:* directive in the view! macro to attach event handlers (e.g., on:click).

    Example of an inline handler:

    view! {
        button(on:click=move |_| counter.set(counter.get() + 1)) { "Increment" }
    }
    let mut counter = create_signal(1);
    let increment = move |_| counter += 1;
    
    view! {
        button(on:click=increment) { "Increment" }
        p { "Count: " (counter) }
    }
  6. Use the new reactivity system in Sycamore v0.8

    main

    Sycamore v0.8 introduced a major breaking change to the reactivity system. The reactive scope (cx) is now explicitly passed to hooks and functions instead of being tracked globally. Additionally, closures no longer require 'static lifetimes, meaning you can access local variables directly without cloning signals into event handlers or effects.

    // New v0.8 syntax.
    let signal = create_signal(cx, 123);
    create_effect(cx, || {
        let _ = signal.get();
    });
    view! { cx,
        div {}
    }