That topic has an example of a self-contained “counter” module called Button.elm. The module’s view and update is used in Main.elm once. Now I’m wondering what the approach would be for reusing Button.elm twice (or more) in Main.elm? How should Main.elm look if it should render two counters that increment/decrement two independent values?
My first thought was your second suggestion: adding a parameter to the message. But that would have meant I need to pass the parameter to the Button view, which I felt would be kind of messy and interfere with the “self-contained-ness” of the Button module.
The trick I didn’t consider was simply defining different message types for the different buttons. That is spot on!
You wouldn’t be passing the extra message parameter to Button.view, as it is used only in Model.view.
Still the extra parameter should be selected so that there are no impossible states. For example Int I chose above is bad example here as it allows e.g. 99 even when there are only two buttons.
If there can be arbitrary number of buttons, then Int can make sense.
Ah I see, you are defining either two distinct new message types (ButtonAMsg, ButtonBMsg) or a new “indexed” message type (ButtonMsg Int Button.Msg). Now I see how you mean that the parameter never has to go to Button.view.
Yes I agree the latter solution (message with index) makes more sense if there can be an arbitrary number of independent counters. Good to know the solution for that case as well!
Just a quick mental heads up in terms of thinking in Elm, in general, you don’t (want? Need?) to think about things as being “self-contained” (e.g. React, etc.).
That’s kind of an anti-pattern in Elm, and will fight TEA (the elm architecture).
Pass as much stuff around as you need! Don’t try to “hide state” in a “component”. Just have one gigantic model with a ton of stuff in it. All good. The compiler will make sure it’s all sorted out! Yay!
@madasebrof Good points! Although… while the compliler might be able to make sense of a humongous model, my poor brain might not (But I haven’t hit that point with Elm yet so my question is just academic, really…)
In general, my main driver for splitting things apart is so that I can work on one problem/feature at a time without needing to scan through or think about (or even scroll past!) things that don’t pertain to the issue at hand. Seems like the advice in the elm-guide of using modules to group things that all pertain to a particular custom type is a good heuristic for that.
Did you watch Richard Feldman’s talk that is mentioned in the post you linked to? I’l link it again here:
Good that you are exploring this ‘academically’ as it is all stuff you need to get your head around to use Elm effectively. Richard’s talk is helpful to build an understanding of if and when you need to do this, and what generally better alternatives there are that should be explored first.