Build Live Component

This guide shows you how to create real-time UI components that update automatically.

Basic Live Component

Create a component by including Azu::Component:

class CounterComponent
  include Azu::Component

  @count = 0

  def mount(socket)
    # Called when component is mounted
    push_state
  end

  def content
    <<-HTML
    <div id="counter">
      <span>Count: #{@count}</span>
      <button azu-click="increment">+</button>
      <button azu-click="decrement">-</button>
    </div>
    HTML
  end

  on_event "increment" do
    @count += 1
    push_state
  end

  on_event "decrement" do
    @count -= 1
    push_state
  end
end

Register Component with Spark

Add your component to the Spark system:

Client-Side Setup

Include the Spark JavaScript client:

Mount a component in your HTML:

Component with Props

Pass initial data to components:

Mount with props:

Handling Form Input

Create interactive forms:

Component Lifecycle

Handle lifecycle events:

Real-time Updates

Push updates from server events:

Optimizing Updates

Use partial updates for better performance:

Component Communication

Components can communicate via events:

Error Handling

Handle component errors gracefully:

See Also

Last updated

Was this helpful?