Replies: 1 comment
-
This is on the hacky side to be sure, but I recently tried this out: class ApplicationComponent < ViewComponent::Base
def set_props(&block)
local_binding = block.binding
local_binding.local_variables.each do |key|
instance_variable_set :"@#{key}", local_binding.local_variable_get(key)
end
yield
end
end and then in a component subclass: class ReadMoreComponent < ApplicationComponent
def initialize(id:, url:)
set_props {}
end
end or adding extra derived "props": class PostComponent < ApplicationComponent
def initialize(post:, show_source: true, hide_hide_toggle: false)
set_props do
@source = @post.source
end
end
end That lets me make instance variables out of all keyword arguments automatically. (Magic!) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First, thank you contributors and maintainers for this great library. I'm so glad there's a solid option for building component based UIs in ruby/rails.
My project has quite a few component classes that do nothing but set instance variables for templates from keyword arguments. For example:
and
I've found myself wanting a
props
accessor, so I can just generate my component class and focus on the template, without needing to add add boilerplate instance variables (like above).If you can remember back a few years to React's class Components, they provided a similar API with
this.props
.Assuming most view_component users are using keyword arguments instead of positional arguments, it would be nice to have something like this in
search_field_component.html.erb
:I've already added this to my project:
But, it would be nice, IMO, if that came "out of the box". Thanks for your consideration!
Beta Was this translation helpful? Give feedback.
All reactions