Passing Callbacks to Live Components to increase usability: yes or no?

I am in the process of upgrading our project to LiveView 0.18. An interesting use case that has popped up is setting up a reusable live component for record forms that handle the Ecto changeset change validation and other boilerplate logic. This reduces the boilerplate by about 80-90%, but in my current implementation requires passing in function captures. The question is, is it an acceptable practice to “select” business logic in a template/component, or should I refactor this to require more boilerplate (probably closer to about 60% reduction) but have business logic purely handled in the parent live view’s handle_event? I could use handle_info as well, but that means that the logic is processed outside of the live event which breaks things like error handling and phx-disable-with.

Here is an implementation of my current approach:

<.live_component module={RecordForm} :let={f} for={@user} change={&Accounts.change_user/2} update={&Accounts.update_user/2}>

So, Live View or Templates?

  • Keep all references to business logic in the Live View
  • References to business logic in a template can be used in moderation

0 voters

I would say go with whatever works for you. I used to make components like these too, the danger with such things is when your component starts to do too many things, you start to lose track of what is going on, so for small scale I always tend to make small custom components, if there are too many of the same kind, refactor to one that is more generic.