Single object attribute or multiple attributes for component
ssssadsadasd
1 month ago
Dec 12, 2024, 7:12 PM
This is probably a trivial question but just wanted to ask. Suppose I have a component named "post". The post displays data of an object which contains such fields as: content, creator object, images etc. Now, this post component is used in a list. My question is: in terms of the speed/efficiency the data appears on screen, should the post have an attribute for each field ( content, creator object, images etc.) or should I simply have an object attribute and then in the component itself show the fields. thanks
Tom Ireland
1 month ago
Dec 12, 2024, 7:26 PM
That's an "it depends" answer. I was reading up on this type of thing recently. See https://cianfrani.dev/posts/web-component-best-practices/ It's opinionated but some good recommendations. I tend to opt for single attributes if I want customisability and something could/will be used outside of toddle. If not, I'll pass in an object or array. The problem with that is you need to be quite explicit with the structure and - if it changes - it can be a pain to sort it out.
Tom Ireland
1 month ago
Dec 12, 2024, 7:28 PM
As an example, for my card component, I'll pass in single attributes for things like button text, the card class, style variables, etc., but provide a specific object for user data related to the card. The component is specific as opposed to being too flexible where any arbitrary data can be passed in, which makes it more complex.
ssssadsadasd
1 month ago
Dec 12, 2024, 7:31 PM
@Tom Ireland thanks. so for my example above, where the object (which comes from a call to a database) and it sometimes can have images sometime not you suggest using an object (since the structure should be the same even if there are no images)
Well, an object would be fine and then you handle cases where the image does not exist within the component. I don't think there's a performance concern either way (don't quote me on that), however once you have your data, it's probably negligble mapping it to individual attributes vs passing in the whole object.