Skip to content

One component, two variations #1210

Answered by boardfish
coder2000 asked this question in Q&A
Dec 15, 2021 · 1 comments · 10 replies
Discussion options

You must be logged in to vote

Hi again @coder2000! You might be interested in using template variants for this purpose. Those docs link to this part of the Rails docs.

If you don't prefer this pattern and would rather specify within your component which component you'd like to render for your slot, a couple of methods come to mind:

  • A lambda slot that's based on the component state, if you'd rather render something different:
class OptionsComponent > ViewComponent::Base
  attr_reader :mobile

  def initialise(mobile: false)
    @mobile = mobile
  end

  renders_many :options, -> { mobile ? ExampleComponent.new : OtherComponent.new }
end
  • Logic in the view file, if you'd rather not render those things at all:
<% unle…

Replies: 1 comment 10 replies

Comment options

You must be logged in to vote
10 replies
@boardfish
Comment options

@boardfish
Comment options

@coder2000
Comment options

@coder2000
Comment options

@boardfish
Comment options

Answer selected by coder2000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants