joshp
Passing a struct to @assigns for a LiveComponent, good idea or not?
I’m updating an app from 1.5 to 1.7 and changing to components/heex, and it’s led me to a question about what the Right Way™ to do something is… Here is the current state of things:
I have a component that generates SVG code representing a gauge. Think speedometer 0-60 kinda thing. This object is updated every 15 seconds with fresh data and redraws.
Currently the way I have things organized to have a module in AppWeb/views/gauge_arc.ex, which contains a struct, a type, and helper functions, including a “build_from_values()” function which takes the numerical values, and populates the remaining fields of the struct with values like “svg_bg_path_string” and “svg_fill_path_string” and “main_label_text”.
In my LiveView I fetch the data, create a new %GaugeArc{} with the basic values, pass that to GaugeArc.build_from_values() to fill in the computed values, and store the entire %GaugeArc{} into the assigns in a variable indicating what the gauge represents. (my page has several GaugeArcs on it)
The LiveView then calls a LiveComponent for each arc, passing in the correct struct from assigns, and the component pulls out the values it needs from the computed fields via @gaugeArc.svg_bg_path_string and the like.
All in all, in phx1.5 land, it’s actually been a pretty reasonable way to move a bunch of data into a liveview, from a code organization perspective.
My questions though:
- I feel like passing a 30-field struct into @assigns for a component is probably… not recommended? The final LiveComponent only uses 13 of them, 3 of which should never change for the life of the component. My assumption is that the correct way to do this in 1.7 would be to create a component with 13 individual attrs, but this still leaves the problem of how to handle getting that data into the assigns in the LiveView itself. 13 attrs each for 4 or more individual arcs. I could have mount/1 create @arc1_val1, @arc1_val2, … variables, but that seems inelegant. Is that the recommended solution for something like this, or was I on the right track with a custom struct to pass into @assigns?
- In 1.7 LiveComponent world, where is the preferred place for the code for these sorts of custom structs or helpers? They are firmly display-only, and generate HTML/SVG code, so I felt that they belonged in AppWeb, not over in App, but I’m not quite sure where to put them in the directory structure. Putting the struct/type/functions directly inside the app_web/components/gauge_arc.ex didn’t seem right. A “helpers” directory perhaps? Curious to see what others do for this.
It’s worth noting that this is a very low-impact project, it will never have more than maybe 10 concurrent users, so sacrificing some performance was never a problem. I’m just trying to think through the Right Way™ so that if I ever run into a bigger project with similar needs I’ve got some ideas, and a better understanding of how te work with change tracking rather than against it. ![]()
Most Liked
sodapopcan
Naw, definitely a case for a live component! I asked because I wasn’t sure whether you really need to keep all your state in the LiveView itself or not. If you did then a live component might not be needed.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










