I’m new to Elm and rewriting an app from React/Redux. Thus far, it’s been immensely satisfying and I’ve been able to find answers on my own except for this problem. That is the situation where I want to take some action when I first conditionally render something and would normally in React use componentWillMount.
In my situation, I have a chat box that is rendered on two routes, but only if either the window size is at desktop width or it’s at mobile width and the chat box “tab” is selected. The behaviour I want is to fetch the chat messages when the chat box is first shown, not when the route is first loaded. I currently have the chat “component” view/update/messages/commands neatly in one separate file which works wonderfully except for initiating this fetch.
I know I could wait for window resize updates and route changes and then only when both conditions are satisfied try to pass a message down to the route update and then chat update function, but that seems like a lot of cognitive overhead that goes into remembering which routes will need the update, etc.
Instead, I’ve left an img tag on the chat view which loads a short gif string, and on load triggers the fetch command. This has worked perfectly but seems like a mega-hack and feels wrong.
chatView model =
div [ class "chat-view" ]
[ img [ src shortBase64Gif, on "load" (Decode.succeed FetchMessages) ] []
...
Is there a simpler solution I’ve missed based on some assumption about how this should be implemented? What might go wrong if I left the code as is?
Thanks for any help