r/angularjs • u/[deleted] • Jun 30 '18
Why React is bad.
Here's why I think Angular is much better than React for creating SPAs.
React is not a framework like Angular is. It doesn't provide a comprehensive, one stop solution for all the things that you need for building an SPA. Often you will hear that React is 'un-opinionated' and that it allows developers the freedom to use whatever technologies they want alongside it. In practise, this means having to either constantly reinvent the wheel, or else employ a hotchpotch of third party libraries and be completely at the mercy of their maintainers.
Third party libraries may be buggy and there's no guarantee that fixes will be swift, if they even happen at all. Plus, they may be deprecated without any notice and you have to hope that they will work together successfully.
For state management Redux is the defacto standard. Redux is, in my view, seriously flawed. Firstly, why do you need to have a store in your client side at all? Is that not what the Database is for? In an SPA, the URL should be the single source of truth and the entire state of the app should be derived from this, using data fetched from a service. When you use Redux in an SPA, you need to work to keep the store in sync with the URL. This is a lot of work for no obvious benefit.
Another problem is that it risks storing stale data. Once the URL changes, all the existing data is potentially stale. There is no value in storing data across requests. If you want to cache data, the browser will cache your requests. Invalidating caches is notoriously difficult to do - why on earth do we bring this problem to client side development!
Redux also introduces Race conditions. If we make multiple requests to the server in quick succession, the Redux store has no way of knowing in what order these were made and given that they can return in any order, it is quite possible that the page we are looking at could be overwritten by the data from an old request! More detail about this problem here:http://blog.richardhunter.co.uk/index.php/13
The Redux store does NOT represent the current state of your app. It probably contains it, but it contains an awful lot besides and working out what is the current state and what is old state involves a lot of error prone work.
The next problem with React is that it does not support Dependency Injection. https://en.wikipedia.org/wiki/Dependency_injection DI is essential for building a complex enterprise grade application. The main benefits of DI are that it makes object creation a much cleaner process and it decouples dependencies from their implementation. If the last point is unclear, what it means that when you build an object you can supply completely different objects for any particular dependency. This is useful in testing where you can swap in mock objects for dependencies and thus test your objects in isolation from each other. With React, you have to rely on monkey patching or else test much larger chunks of your code at a time.
In React, you have to pass any dependencies down to components through props which can mean that a component has props that it doesn't actually use itself but just has them so it can pass them down to child components. You can use the Context for surfacing dependencies in a component but this is very much a poor man's DI imho.
There are other things about React that I don't like, but I don't want to extend this post too much. In particular PropTypes seem like an extremely feeble way of doing static typing and really typifies what is bad about React.
React isn't just different for not being a framework - it's worse. It is hopelessly inadequate for doing the job that it is supposed to and just results in messy, buggy, and inadequately tested applications.
I think the reason that React is more popular than Angular is because Angular lost a lot of good will during the shift from version 1 to 2. Maybe the Angular team didn't cover itself in glory, but I think the decision was the right one as the new Angular is much superior to the old one. It's irrational to dislike Angular because of these events but maybe a lot of developers are irrational.
25
u/prof_hobart Jun 30 '18
A client-side cache is there to improve performance - faster client responses, less server load and less fragile to intermittent networks. If you don't need any of that, you possibly don't need a client cache. There's absolutely nothing in React's structure that prevents you pulling the required state from the server on ever page navigation. And there's nothing to stop you using the browser cache if that's what you want to do. But that's a lot more complex than simply building up a client-side model of the data your app needs.
If you're doing multiple async calls at the same time that can return contradictory data, then yes this is something you're going to deal with. But I'm not sure how that's got anything to do with React - it's been true as long as I've been working on async. There's things that you can do to help, but again mostly nothing language-specific, such as
redesign your system so that you don't have this possibility (it's the kind of thing that backend-for-frontend orchestration tiers are there to help with - create a single server-side endpoint that pulls data from multiple sources and returns it in one go)
have your client-side logic know which data should take precedent, whether based on which call it was, what the timestamp of the call was, or something else
wait for call A to come back before kicking off call B
There's things that libraries can help with (redux-saga, for instance, makes some of the coordination easier), but fundamentally these are just basic problems of asynchronous traffic - if you do two simultaneous calls, you've got no direct control over what order they'll come back in. Or is there something magical that Angular does to make this go away?
Don't understand what you mean. If you're designed your model badly, or don't understand the nature of your server interactions, then yes you're going to have problems. But in a well-designed app, this is one of the things that Redux makes ridiculously easy - take the current state and apply a transformation to it based on the most recent action and produce a new state. It's basically functional programming, making it easy to both reason about and test.
This is one of the other things that Redux makes very easy - at the appropriate point in your component hierarchy, pull out the bits of state (and potential actions) that it needs to worry about and pass them as params.
It doesn't have a whole framework around dependency injection, no. But there's nothing stopping you simply injecting your dependencies through parameters if that's what you want to do.
There's plenty of problems with React. But most of these seem little to do with React at all and the ones that do seem to be based on a flawed understanding of what it provides.