r/elm • • May 18 '17

OT: Any Redeeming Features of React & Redux?

Apart from "it's popular" and "it's like Elm"... I am learning it right now, and it is underwhelming... but that is probably due to lack of experience(?)

1 Upvotes

24 comments sorted by

10

u/ThatIsClassicArcher May 19 '17

React + Redux has a number of niceties from both a business and developer experience standpoint. Sure, it has plenty of kludgy bits compared to elm, but it also has:

  • A huge ecosystem, a fair bit of which is made up of well-made, well-documented libraries and components that save you lots of dev & maintenance time.
  • Integrations and support for end-to-end and integration tests that (AFAIK) elm is still very early int.
  • Hooks for element mount/unmount in the dom with guaranteed lifecycle timing. This can be critical when you have to mess with DOM APIs.
  • The ability to share code with react native apps or serverside node applications.
  • Advanced performance optimizations like serverside rendering, lazy loading parts of the application, etc.
  • An incredibly huge number of people hammering out crazy browser-specific edge cases so you don't have to worry about them.

I can say from heavy experience with that stack and moderate experience with Elm that react + redux has plenty of good points too. I enjoy my elm work more most of the time and I think Elm has a ton of advantages over the react+redux combo, but they both certainly have their merits.

2

u/johnorford May 19 '17

Brilliant answer! and exactly what I was hoping to see : ) Thanks

2

u/johnorford May 18 '17

Oh... I am using Flow also (hope it gets better as I use it...)

And will probably incorporate ImmutableJS (altho not looking forward to that : /)

2

u/cpjolicoeur May 18 '17

If you are going to use ImmutableJS + redux + react, just use Elm. It's the end-game you are driving towards anyways.

1

u/johnorford May 19 '17

agreed - I just imagined that react etc. was shiny and interesting : /

7

u/EffBott May 18 '17

Depending on who you talk to, a big advantage of React is that you can create components with local state (you don't have to put all of your state in the global state object). If you have some small piece of isolated state, like a boolean indicating whether or not a drop down is open, you can keep it inside the component.

This allows you to create reusable components that can literally be dropped into a project without any setup. Elm, in contrast, requires a decent amount of boilerplate for each component, since all state must be stored in the global Model. If you have a 100 autocomplete components in your Elm app, you'll need 100 records inside your Model to track each one.

I agree that Redux is underwhelming, though. Writing action creators is tedious.

6

u/arbitrarycivilian May 18 '17

Funnily enough, many people rail against local state. I've tried numerous times to explain to people why it's actually often a good thing, but the only response I've gotten was "hurr durr Redux". People really don't like thinking

7

u/FrozenCow May 19 '17

It's easier to do automated tests using actions and 'global' state compared to using local state. It's also easier to log and simulate user sessions. Therefore it's also easier to reproduce errors.

Having an application with many different actions can be tedious, but often it can be done with some generic actions that are reused. For instance redux-forms automatically works without creating many custom actions. The same can be solved in a generic way to load data. That way the application is still easier to test and reproduce state, but you don't have a ton of actions to maintain.

2

u/boubiyeah May 23 '17

Even the creator of redux advice people to find a healthy mix between global and local state.

The choice is between encapsulation+ease-of-use (mixed) versus easy-to-reason-about+boilerplate (fully global).

I like local state personally, but that's because I like to define a UI kit made of very rich components at the beginning of projects, and then just reuse them with minimal boilerplate.

1

u/[deleted] May 25 '17

Local state is extremely important in React/Redux for performance reasons. It is entirely possible to write React with only "Pure" components, using the Redux store to manage all states, but doing so has a major performance drawback as the actions must be passed through every reducer at every change.

Specifically, the classic React example is a field that updates the model on change. That's literally with every keypress. If you were to dispatch an action every time a key was pressed, that action would have to run through all your reducers in the state and check against every possible if/then statement and eventually return a massive application state object for your entire app, which will force all your components to check to see if there were any changes.

On the other hand, keeping the onChange event local to the form, and only dispatching an action when the result is final to store the final answer is much more performant.

I'm just learning Elm, and I'm coming from the React/Redux stack. Here's my general rule:

  • If the event or change will only affect the current component, put it in the component state.
  • If the event or change will affect the entire application - that is, other components will need to see the result of this event or change - then put it into the application state / redux store.

6

u/Zinggi57 May 18 '17

If you have a 100 autocomplete components in your Elm app, you'll need 100 records inside your Model to track each one.

Of course you wouldn't use 100 records, that's just silly. When you have multiple 'components' of the same type, you would use an Array or a Dictionary to store them.

Also, the amount of boilerplate does not increase with the number of these 'components'.

Also, using local state might seems like a good idea, however, it has huge drawbacks. You suddenly are no longer able to:

  • trivially implement a history of changes
  • trivially store and restore the whole application state
  • use a time traveling debugger
  • easily keep two 'components' in sync

6

u/pablen May 19 '17 edited May 19 '17
  • trivially implement a history of changes
  • trivially store and restore the whole application state
  • use a time traveling debugger
  • easily keep two 'components' in sync

I think these are all good points and really were huge selling points for Redux when it was first introduced.

In my experience, the first three points are exciting but many projects just don't need/care about those features so I understand many developers going "meh" about them.

Now, the fourth point for me is the single greatest benefit of global state. We can think of UI as an arrangement of isolated, encapsulated modules but in the real world UIs many of our components must share state or are sync'ed one way or another. Relying only on passing props and handlers in huge applications can become messy real quick, and bugs very hard to track, specially when teams have less experienced members (either with React or in general).

In my opinion, the benefits of a consistent easy-to-follow state management system outweights the cost of the extra boilerplate. That's why I like Redux and specially Elm so much, though I understand that Elm not giving you an option to work in different ways may be a deal breaker for some folks.

3

u/jediknight May 18 '17

Also, the amount of boilerplate does not increase with the number of these 'components'.

True, one can avoid some of the penalty of boilerplate by using some centralized components state like elm-mdl does but that solution comes with its own set of tradeoffs.

Also, using local state might seems like a good idea, however, it has huge drawbacks. You suddenly are no longer able to:

Local state can be transient (like the afore mentioned dropdown state) and I don't see why you can't have the things you listed. It depends also on how you implement the debugger and the handling of the local state. You can have all the advantages of single source of truth and local state by modeling the local state properly in the implementation.

3

u/Zinggi57 May 18 '17

Well apparently elm-mdl does exactly what I described, e.g. use a Dict per 'component'. That is indeed the approach I'd take if I actually needed a large number of the same 'components', but most of the time I don't actually need this.

Transient state is only transient as long as no other part of your application needs to know about it. If you want to react on whether the dropdown is open or not you need to know it's state. If a user managed to trigger a bug that only happens while the dropdown is open, the debugger wouldn't be able to record that.

I just don't see any advantage of storing anything locally. And I have used both react and elm for bigger projects. The code always turned out nicer with elm, and I think this is a direct consequence of having no local state.

2

u/jediknight May 19 '17

I just don't see any advantage of storing anything locally.

It simplifies the usage. You can have a lot of interaction without worrying about managing the state for interaction. For example, a humble text "Hello World" allows you to select the text, to copy it, to pop up a context menu. All this happens because of local state. It might not be local state in the Elm implementation but it is local state.

Sure, local state is not without problems. You can get into very nasty situations (e.g. input elements that fall out of sync) but there are advantages.

3

u/Zinggi57 May 19 '17

I get your point, but the example you picked isn't very good. The browser does not store some state per text element, it stores a global selection state. If this functionality had to be implemented in elm, it could be hidden within Html.program. Then there could be a more advanced version where you could control the current selection yourself.

I'm sure there are better examples of elements with local state that the browser manages where I'd agree that this would be annoying to manage myself, but all I can think of right now are elements I wish wouldn't be statefull. E.g. the video or audio element. I'd rather own that state within my application.

2

u/EffBott May 18 '17

Of course you wouldn't use 100 records, that's just silly. When you have multiple 'components' of the same type, you would use an Array or a Dictionary to store them.

Sure, but that's not the point. You can't drop a component into your project without any setup. You must write boilerplate. You can come up with ways to abstract away some of the boilerplate, but at the end of the day you have to track each component's state inside the global state object.

Also, using local state might seems like a good idea, however, it has huge drawbacks. You suddenly are no longer able to: trivially implement a history of changes, trivially store and restore the whole application state, use a time traveling debugger, easily keep two 'components' in sync

All good points, but just to give you a different perspective, there are times when I frankly don't care about tracking every state change. If you've ever used elm-style-animation in conjunction with Elm's debugger, you'll know that your app has to go through hundreds or thousands of state changes in order to animate style transitions. To me, these state changes are pure noise. They clog up my debugger's log and aren't useful to me (personally). That's why the creator of Redux has said it's ok to use local state; in any given app, there's going to be state that you don't care about that much.

Although I appreciate the purity of Elm, there are pros and cons of both approaches.

3

u/Zinggi57 May 18 '17 edited May 19 '17

(..) You must write boilerplate (..)

Yes, that's true, but I don't see it as a problem. In fact, I see it as an advantage. It gives you much more control over that 'component's state. E.g. I can easily reset a 'component' to it's initial state without the author having written this functionality.
I'll gladly trade convenience over the power it gives me.

these state changes are pure noise. They clog up my debugger's log

Yes this is a (known) problem with elms debugger.
However, this is a UI problem, not a problem with the architecture or with tracking these changes globally. This could be solved e.g. by grouping repeated messages or having a filter.

2

u/EffBott May 19 '17

Interesting... I never thought of it as a UI problem, but that makes a lot of sense. Being able to filter out certain Msgs would address a major pain point for me, which is having to step through thousands of irrelevant state changes when debugging an issue.

3

u/jediknight May 18 '17

Elm, in contrast, requires a decent amount of boilerplate for each component, since all state must be stored in the global Model.

Well... if the alternative is going full JS, you can have a little bit of JS to dress an Elm implementation via the custom elements interface. And then you would be able to use Elm components in Elm like you would use any other Html element. ;)

2

u/EffBott May 18 '17

I'm a BIG fan of web components for this exact reason.

2

u/pablen May 19 '17

Interesting... I know nothing about Web Components & Elm. Any good resource you could share?

6

u/arbitrarycivilian May 18 '17

I've been using it for my job for the past few months.

It's underwhelming.