Context Passing with View Components #1345
-
Hey team! I've been getting into the nuts and bolts of view components recently, and I've really enjoyed poking around your codebase. I've run into a question about view contexts that I wanted to ask here. Do view components create a new As a second part to this question, if view components do create new view contexts, is it possible to pass information through contexts in a similar fashion to React? I'm developing something right now that would require what is essentially a fair amount of prop drilling that I wouldn't mind avoiding if possible. Thanks for your help! Always enjoy poking around the great work that you all do at GitHub. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @jacobdaddario, great question! I think I can answer some of this. I'm fairly confident components do create their own The reason why the method signature on To answer the second part of your question, I wouldn't recommend sharing information via renders_one :button, -> { ButtonComponent.new.with_content(@title) } <%= render MyComponent.new do |c| %>
<% c.button %>
<% end %> renders_one :button, -> { ButtonComponent.new } <%= render MyComponent.new do |c| %>
<% c.button do %>
<%= c.title %>
<% end %>
<% end %> |
Beta Was this translation helpful? Give feedback.
Hey @jacobdaddario, great question! I think I can answer some of this.
I'm fairly confident components do create their own
view_context
s. This helps the library to be as fast as it is, but also gives some view helpers unexpected issues, likeform_with
. There's experimental support for the global view context, which would come from the view in which you're rendering components.The reason why the method signature on
initialize
is a bit different is likely because instances ofViewComponent::Base
respond torender_in
. When you callrender MyComponent.new
, that's callingrender_in
on the instance ofMyComponent
and passing in the view context, if my understanding of it is correct.To answer …