Link Search Menu Expand Document

ViewComponent

A framework for creating reusable, testable & encapsulated view components, built to integrate seamlessly with Ruby on Rails.

What is a ViewComponent?

ViewComponents are Ruby objects used to build markup. Think of them as an evolution of the presenter pattern, inspired by React.

ViewComponents are objects that encapsulate a template:

# app/components/message_component.rb
class MessageComponent < ViewComponent::Base
  def initialize(name:)
    @name = name
  end
end
<%# app/components/message_component.html.erb %>
<h1>Hello, <%= @name %>!</h1>

Which is rendered by calling:

<%# app/views/demo/index.html.erb %>
<%= render(MessageComponent.new(name: "World")) %>

Returning:

<h1>Hello, World!</h1>

When should I use ViewComponents?

ViewComponents work best for templates that are reused or benefit from being tested directly. Heavily reused partials and templates with significant amounts of embedded Ruby often make good ViewComponents.

Why should I use ViewComponents?

Testing

Unlike traditional Rails templates, ViewComponents can be unit tested. In the GitHub codebase, ViewComponent unit tests are over 100x faster than similar controller tests.

With ViewComponent, integration tests can be reserved for end-to-end assertions, with permutations covered at the unit level.

For example, to test the MessageComponent above:

class MessageComponentTest < GitHub::TestCase
  include ViewComponent::TestHelpers

  test "renders message" do
    render_inline(MessageComponent.new(name: "World"))

    assert_selector "h1", text: "Hello, World!"
  end
end

ViewComponent unit tests leverage the Capybara matchers library, allowing for complex assertions traditionally reserved for controller and browser tests.

Data Flow

Traditional Rails templates have an implicit interface, making it hard to reason about their dependencies. This can lead to subtle bugs when rendering the same template in different contexts.

ViewComponents use a standard Ruby initializer that clearly defines what is needed to render, making reuse easier and safer than partials.

Performance

Based on several benchmarks, ViewComponents are ~10x faster than partials in real-world use-cases.

The primary optimization is pre-compiling all ViewComponent templates at application boot, instead of at runtime like traditional Rails views.

For example, the MessageComponent template is compiled onto the Ruby object like so:

# app/components/message_component.rb

class MessageComponent < ViewComponent::Base
  def initialize(name:)
    @name = name
  end

  def call
    @output_buffer.safe_append='<h1>Hello, '.freeze
    @output_buffer.append=( @name )
    @output_buffer.safe_append='!</h1>'.freeze
    @output_buffer.to_s
  end
end

Code quality

Template code often fails basic Ruby standards: long methods, deep conditional nesting, and mystery guests abound.

ViewComponents are Ruby objects, making it easy to follow (and enforce) code quality standards.

Contributors

ViewComponent is built by over a hundred members of the community, including:

asgerb bbugh bigbigdoudou blakewilliams bobmaerten bradparker cesariouy cover czj dark-panda davekaro dukex dylnclrk elia franco franks921 fsateler fugufish g13ydson horacio jaredcwhite javierm jcoyne jensljungblad joelhawksley johannesengl jonspalmer juanmanuelramallo jules2689 kaspermeyer kylefox manuelpuyol mattbrictson maxbeizer mellowfish metade michaelem mixergtz mrrooijen nashby nicolas-brousse nshki nshki rainerborene rdavid1099 rmacklin seanpdoyle simonrand skryukov smashwilson spdawson Spone swanson tbroad-ramsey tclem tenderlove tonkpils traels vinistock xronos-i-am matheusrich Matt-Yorkley ryogift andrewjtait websebdev xkraty wdrexler mattwr18


Getting started →