You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been playing with the idea of using View components as a way to prepare data for other components in a view, similar to how React uses Containers to prepare data for idempotent Components.
In the following example:
ProdutsGridContainer has the responsibility of grabbing all the data using any means necessary (ActiveRecord for example).
ProdutsGridContainer's test isn't focused on the actual HTML renderred
CardComponent is idempotent
CardComponent doesn't have the need to query any more data. It receives primitives such as strings, integers etc.
CardComponent's tests are super light because they don't need any database access.
classProductsController < ApplicationControllerdefindex@products=Product.with_attached_image# uses ActiveStorage to bring in the attachmentsendend
classProdutsGridContainer < ViewComponent::Basewith_collection_parameter:productdefinitialize(product:)@product=productenddefcallrenderCardComponent.new(title: title,description: description,image: image_url)endprivatedeftitleproduct.nameenddefdescriptionproduct.descriptionenddefimage_urlproduct.image.url# =>this is a call to a related model, not Product itselfendend
Have you encountered this need in your projects? How do you feel about this pattern in Rails?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hey!
I've been playing with the idea of using View components as a way to prepare data for other components in a view, similar to how React uses Containers to prepare data for idempotent Components.
In the following example:
ProdutsGridContainer
has the responsibility of grabbing all the data using any means necessary (ActiveRecord for example).ProdutsGridContainer
's test isn't focused on the actual HTML renderredCardComponent
is idempotentCardComponent
doesn't have the need to query any more data. It receives primitives such as strings, integers etc.CardComponent
's tests are super light because they don't need any database access.Have you encountered this need in your projects? How do you feel about this pattern in Rails?
Beta Was this translation helpful? Give feedback.
All reactions