I have had the “pleasure” of porting an Elm app to a React app when leaving my previous job. You’re right that it maps relatively easily, at least much easier than React to Elm.
Surprisingly, I found that Redux wasn’t as smooth a mapping as I thought it would be. Redux handles the state, and should be made of pure functions to work well. That seems to be a perfect fit for Elm’s
update, but the difference is in how the commands or side-effects are handled.
In React/Redux, code that triggers a “Msg” only sends a message to Redux to update the state. If there is a need to trigger a command like an HTTP request, I found that a more suitable place was to have that triggered next to the call to the Redux update function, because that should not be done in the “reducer”.
There are a few side-effects to that:
- Commands are not as centralized as in Elm
- Redux messages say “how the state should change”, rather than “what happened”. “IncrementCounter” vs “IncrementButtonWasClicked” if you will.
Because of these differences (and probably more that I forgot), it’s not a one-to-one mapping and the result doesn’t feel as nice as Elm’s.
I also remember that React requires you to set keys to HTML elements when you’re mapping over children and can get into weird states if you don’t (and get warnings in your console), whereas Elm only makes that an optional optimization.
what even is the recommended way of writing React apps now a days? Do people still use Redux?
I don’t know, I haven’t followed React enough since working with Elm full-time. That said, there are plenty of flavors for Redux out there to deal with the results of, like
redux-saga, so even if React/Redux is the compilation target, there would be a need to choose one flavor, or to go with a custom one.
I don’t know whether I would advise you to target Redux or instead advise not to use Redux and instead replicate Elm’s TEA, which is relatively simple anyway, for the reasons I listed above. I feel like that would be a bit simpler.
I think I’ll keep working on my own projects (much to do with
elm-review), but I have to admit I had an unexpected amount of fun when rewriting the Elm project to React (though that may have been because I like doing this architectural kind of work and I also tried doing it gradually so that we could still ship things to production). So if this sounds interesting to someone, go for it!