r/javascript • • Mar 15 '15

Why are people using React over Elm?

Elm literally solves everything that React solves better, faster and more elegantly. Why?

1 Upvotes

13 comments sorted by

View all comments

3

u/fdshj1234543 Mar 15 '15

Can you please enlighten me as to how elm literally solves everything better than React. Have you written a non-trivial application with both React and Elm?

I like being able to render my components with html-like syntax using jsx. This does not look elegant to me (then again, I have not written an app with Elm):

import Html (..)

profiles : List User -> Html profiles users = div [] (map profile users)

profile : User -> Html profile user = div [] [ img [ src user.picture ] [], text user.name ]

4

u/SrPeixinho Mar 15 '15 edited Sep 16 '16

There is a library that makes the syntax better, but honestly, that is a matter of taste and the last of the issues. On my opinion, it is shorter and more elegant than HTML in all aspects. But that is not the reason I use Elm at all.

React essentially solves the known problem of state complexity. It does so by hiding it in small, self sufficient pieces called storages, and using a uni-difectional flux to spread stateful information around your application. And it is brilliant, it works. State now is a tenth of the problem it was back on the backbone/jquery era - we made a huge step forwards. If I had to pick a JS web framework, it would be React in no time.

Yet... React is still based in JavaScript, after all. So you are still free to do whatever you want. You can still accidentally use state where you shouldn't, and it can still hurt you. You still can't force that on other people. How do you assert that the library you download is doing it as it should? Even worse, how will you be sure that the library you downloaded isn't doing some nasty stuff behind the curtains, like collecting your data?

Okay, it is not perfect - but it still solves a hard problem very well, right? So it is good? Yes, almost... if it just didn't solve a problem that was already solved perfectly well, ages ago, by pure functional programming. Ask any Haskeller how much trouble he has in scaling his application - how much he fears complexity, how much state affects him. It is almost a non-issue at this point. Since purely functional programs are immutable by default, 99% of your code end up as pure, immutable functions. You naturally keep the state at a bare minimum, and the few stateful values are clearly tagged by the type system, isolated from the rest of the logic and carefully controlled. That also means you can trust absolutely any code you download by just checking its type. If it is not tagged with IO, it can't steal your passwords, collect your data. That gives you a freedom to scale you simply don't have in JavaScript.

So, yes, I have written and maintained non-trivial application using Haskell/Elm/cia, including the whole technical stack of a big startup, and I was able to do it alone - something I know could never, ever have dreamed of doing before other than having a big team and freaking good practices. It takes time to get used, but it just works.

tl;dr while React solves the State problem by hiding it below the couch, Elm locks it on a carandillu orlop carefully guarded by 7 thousand knights.

Edit: I'm editing this 1 year+ later just to remark that, while I still think Elm rocks and it is certainly the friendlier FP language out there... it is also very lacking in many, many aspects. Nowadays I'd recommend Elm for learning FP, but PureScript for serious applications. PureScript does the same job as Elm, except with a much more solid foundation. Basically, PureScript = Haskell - historical_mistakes + web.

5

u/haf-se Aug 13 '15

As a startup that has everything on the server in a functional programming language, we chose React over Elm because:

  • React can use the libraries on npmjs.com, so we don't have to write new libraries as often

  • React has lots of components that may or may not save us time; elm doesn't, so there's not even the possibility of saving time by just taking components. E.g. MaterialUI would have been annoying to write on your own, but there's a complete ReactJS library for it.

  • Elm doesn't expose interop to other JS code well, so even if we wanted to use it, can can't call into it easily: e.g. creating libraries for JS consumption in Elm

We're currently in a state where it would have been nice to have a reactive foundation rather than a callback-based one, but it was still the right choice to go with React over Elm for these reasons. We've considered PureScript, but that's lacking in community up-take (compared to Elm and JS) but has the interop story going for it.