LiveComponent is a client-side rendering and state management library for ViewComponent.
We ❤️ ViewComponent
Render your existing view components from the browser by updating their state, just like you would in React or Vue. Call methods on your components to keep logic in Ruby.
Seriously Fast
Although rendering is done server-side, LiveComponents typically render in ~50ms or less by avoiding Rails overhead. LiveComponent tracks your component's state so re-renders can often be done outside the controller.
Built on Stimulus
Leverage the techniques you already know for adding rich interactivity to Rails. LiveComponent is built on Stimulus and designed to work seamlessly with Hotwire.
1 | <div class="TodoItem"> |
2 | <% if @editing %> |
3 | <%= form_with(model: @todo_item) do |f| %> |
4 | <%= f.text_field :text %> |
5 | <%= f.submit %> |
6 | <% end %> |
7 | <% else %> |
8 | <%= @todo_item.text %> |
9 | <%= button_tag( |
10 | "Edit", |
11 | data: { action: "click->todoitemcomponent#edit" } |
12 | ) %> |
13 | <% end %> |
14 | </div>
|
1 | <div class="TodoItem"> |
2 | <% if @editing %> |
3 | <%= form_with(model: @todo_item) do |f| %> |
4 | <%= f.text_field :text %> |
5 | <%= f.submit %> |
6 | <% end %> |
7 | <% else %> |
8 | <%= @todo_item.text %> |
9 | <%= button_tag( |
10 | "Edit", |
11 | data: { action: "click->todoitemcomponent#edit" } |
12 | ) %> |
13 | <% end %> |
14 | </div>
|
1 | class TodoItemComponent < ApplicationComponent |
2 | include LiveComponent::Base |
3 | |
4 | def initialize(todo_item:, editing: false) |
5 | @todo_item = todo_item |
6 | @editing = editing |
7 | end
|
8 | end
|
1 | class TodoItemComponent < ApplicationComponent |
2 | include LiveComponent::Base |
3 | |
4 | def initialize(todo_item:, editing: false) |
5 | @todo_item = todo_item |
6 | @editing = editing |
7 | end
|
8 | end
|
1 | import { live, LiveController } from "@camertron/live-component"; |
2 | |
3 | @live("TodoItemComponent") // this is the Ruby class name |
4 | export class TodoItemComponent extends LiveController { |
5 | edit() { |
6 | this.render((component) => { |
7 | component.props.editing = true; |
8 | });
|
9 | }
|
10 | }
|
1 | import { live, LiveController } from "@camertron/live-component"; |
2 | |
3 | @live("TodoItemComponent") // this is the Ruby class name |
4 | export class TodoItemComponent extends LiveController { |
5 | edit() { |
6 | this.render((component) => { |
7 | component.props.editing = true; |
8 | });
|
9 | }
|
10 | }
|