r/javascript • u/rrrosenfeld • Jun 16 '17
Adopting React.js seems risky for long-term projects
https://rosenfeld.herokuapp.com/en/articles/2017-06-16-adopting-react-js-seems-risky-for-long-term-projects22
u/acemarke Jun 17 '17
Your complaint seems to be based on a misunderstanding of how React re-renders.
As you noted, the "Integrating with Other Libraries" page says:
To prevent React from touching the DOM after mounting, we will return an empty <div/> from the render() method. The element has no properties or children, so React has no reason to update it, leaving the jQuery plugin free to manage that part of the DOM
Remember that React only compares the Virtual DOM trees returned by your component (ie, the last output from render vs the current output) to determine if any DOM changes are needed. It does not compare the render output with the actual DOM to make that decision.
Therefore, if your component's render method is simply return <div />, no matter how many times your component re-renders, React will always see that the component is trying to render one single div, and after the first render there will be no changes made whatsoever. That means that you can safely append non-React HTML nodes to that div, and React won't try to remove them. That's why you can use the pattern of initializing jQuery plugins and other DOM-manipulation-based libraries in componentDidMount, because React has done the work of creating that initial requested element, and as long as your component doesn't tell React it wants anything different, React won't make any changes.
I also don't understand your "autocomplete" example. React's defined semantics are that as long as you keep rendering a given component type in the same place in your render method, React will keep the matching component instance mounted. That's why, if you only use array indices for keys in a list and then make changes to the array contents like deleting an item, you will see already-mounted list items suddenly having their props change (because React thinks you're asking for the same component with different props than before).
I do find the note about possible changes to shouldComponentUpdate to be a bit curious, but given the size and scope of the React ecosystem, and the importance of sCU as a performance optimization, I find it very unlikely that there will be any changes to its semantics in the foreseeable future. In addition, the React team is extremely serious about maintaining compatibility and providing upgrade paths, such as releasing automated codemods to update your code to newer patterns.
Overall, while I understand your wanting to do due diligence and keep yourself from being locked-in, I think you are really over-thinking things :)
0
u/rrrosenfeld Jun 17 '17
The problem with that snippet of the documentation is that it's contradicted by this other snippet:
It is important to remember that the reconciliation algorithm is an implementation detail. React could rerender the whole app on every action; the end result would be the same.
The result won't be the same if the reconciliation altorithm changes in such a way that React would rerender the whole app, that's what I'm really complaining about. The documentation has statements that are not compatible one with another.
As long as the reconciliation algorithm would never perform unneeded DOM operations, shouldComponentUpdate is not even required in order to being able to integrate with third-party components. I mean, with the current algorithm it's possible to always integrate with third-party components but I'm worried about React saying that it could replace its algorithm so that it would re-render the whole tree and it would be still fulfilling its specifications.
I understand about React's keys for lists and I do have unique keys to use in my application, this is not my concern. I mean, I'm just fine with the current React implementation. I'm not fine with the fact that React doesn't provide any hard constraints about its reconciliation algorithm so that I could count on it for future releases.
In addition, the React team is extremely serious about maintaining compatibility
But my concern is that since it's not part of the specification, React doesn't have to worry about compatiblity in that case.
I also don't understand your "autocomplete" example.
Let me explain how things actually work in my application. Users can search for deals based on criteria created on a set of 800 or so fields structured in a nested tree component. Sometimes, some of those fields only apply under certain circumstances. So, if the Consideration Type field includes the value "Cash", then some of the fields in the tree would be displayed (or hidden). As the autocomplete menu is open, several options are displayed with a checkbox next to each of them. As those options are checked the menu remains open but the changes take place immediately as the search results reflect the new values and the fields tree state is also changed accordingly in order to hide or show other fields based on the value section for the field being edited in the autocomplete. Since setState in an ancestor element will trigger its render method, if the reconciliation algorithm changes in such a way it doesn't understand correctly that some nodes shouldn't be touched, then there's a chance that the node being edited would be removed and re-added, and in that case the user would see the autocomplete menu closing when it shouldn't. That's my concern.
12
u/acemarke Jun 17 '17
You're still kind of misunderstanding, I think.
Even if React re-rendered the entire application, as long as your render output doesn't change, then React won't alter the currently mounted components or DOM nodes. For your use case, it's not the re-rendering that's a potential concern, it's whether components might be unmounted, and they'll only be unmounted if you ask React to render something sufficiently different. "Rendering" means "return a tree of objects describing how I want the UI to look like now". It's a prerequisite for React modifying the DOM and component tree, but simply re-rendering doesn't mean it will make changes.
7
u/rrrosenfeld Jun 17 '17
You were right, /u/acemarke. React team responded in that issue:
When documentation says "re-render" it refers to calling the render method and reconciling the changes, not unmounting and mounting components. React never unmounts components unless key or type changes.
While we can't guarantee that React won't be calling render in more cases, we can pretty much guarantee that:
If component key and type doesn't change, React won't unmount the component (and destroy its state). At most, it will attempt to reconcile parts of the DOM managed by React. And you can always short-circuit that by returning just <div /> and managing its content yourself. If you manually touch the DOM that React doesn't manage (e.g. imperatively change an attribute that doesn't exist on React side), React won't blow away your changes on re-rendering.
This is pretty enough guarantee to me and I'll try to submit a PR to make that part clearer in the reconciliation documentation. I'll update my article first. Thanks :)
3
Jun 17 '17
/u/acemarke I think /u/rrrosenfeld has a fair point that the reconciliation doc has this unnecessarily scary statement that "React could re-render the whole app on every action; the end result would be the same." which has a contradiction with uncontrolled (or controlled by external jQuery plugins) components in general.
If in my view tree I render a slider whose value I don't track, what happens if React decides to render my entire subtree? If the user has moved the slider from 0 to 50, and I refuse to track the value in code, what happens now? When the user sees the component again, who will move the slider back to 50 where the user left there.
This crazy example will of course never happen in production, because React does not arbitrarily make decisions and decide to make different rendering decisions, whether they say reconciliation is an implementation detail or not.
Now what I don't understand from /u/rrrosenfeld is that how can you ever trust a third-party library with this paranoiac mentality? It seems you're a bit too risk-averse, and I cannot see you taking a dependency on a library whose documentation -in my opinion- is a bit shaky. Try to use react-native and you will even see that not all props that React's views accept are undocumented and you will have to dig in their code base to implement more complex interactions.
Lastly, no one makes you update react versions. It does follow semver, so if there's a breaking change you will likely see while updating from 16 to 17 and 17 to 18 and so on. For a long term project it'd be better not to trust internals of react but write your application in a way you can test and see if things break. I've worked in a Rails 2 code base before and the team could not update to Rails 4 because they knew they would break. It happens, projects get rewritten, dumped, completely refactored, you got to take action at some point.
7
u/acemarke Jun 17 '17
I agree that the statements regarding possible changes to future behavior raise a couple questions about what might happen, but as I've already said a few times now: re-rendering is not the issue! If you have a component that wraps up a jQuery plugin, and only ever renders a single
<div>, then re-rendering will not change that div or interfere with the elements the jQuery plugin has appended.1
Jun 17 '17
Maybe we have a conceptual difference when we both mention re-render. If we're talking about re-render as in calling the
rendermethod which is part of aReact.Component, I absolutely agree from you.What I understood from that "React could re-render the whole app on every action; the end result would be the same." sentence was, we will dump away your entire tree and re-create it, and you should be able to recreate it from scratch, only with the initial props given to you. As in, make sure your view tree can be represented as a function of input (props) to output (components) without outside influences. In that case I imagine that single
<div>instance going away.10
u/acemarke Jun 17 '17
Yeah, "rendering" does have a very specific meaning - calling
render():) So, when the React team talks about "re-rendering the entire app", I am about 99.99% certain they mean recursively walking down the component tree and callingrender()on each component, not "blow it all away and start fresh".As mentioned, the React team is very pragmatic, and React does have all these "escape hatches" like refs and
shouldComponentUpdateexplicitly so that React can interop with non-React code and skip unneeded work, as you see fit to use them for your app.1
u/rrrosenfeld Jun 17 '17
/u/acemarke was right with regards to what the documentation meant when talking about rerendering:
https://github.com/facebook/react/issues/9926#issuecomment-309205972
I just updated my article to reflect that.
2
u/rrrosenfeld Jun 17 '17
The main issue with React documentation has been clarified already by the React team and I can now become confident enough to adopt React, I'll just answer to some statements.
Now what I don't understand from /u/rrrosenfeld is that how can you ever trust a third-party library with this paranoiac mentality?
I'm not paranoic about every component, but something like React, Angular or Knockout, or jQuery, specially if they are used all over the application, should be considered a critical part and for those cases I'm very careful before adopting such kind of technology. I can easily replace some common datepickers, sliders or autocomplete components, so I don't need to be paranoic about those, so I don't have a problem with trusting third-party libraries in general because I don't actually have to trust them. I can easily replace them if they become an issue. But how am I supposed to replace something like React if it can't play nice with other technologies? Now that I understand this is not the case, I can feel safe to adopt it knowing that I'm free to change my mind later with a feasible migration path from React if I decide to move away from it.
Lastly, no one makes you update react versions.
Indeed, but no one wants to use an ancient version forever. If the new version breaks something by mistake, it will be fixed in the next version and I can wait before upgrading. However, if the change was intentional and it no longer allows you to integrate with third-party libraries, then that means you'll have to move away from it which will cost you quite some time. If you already know this is supposed to happen, why do that? That was my point. Fortunately this wasn't the case. I just misinterpreted the docs.
I've worked in a Rails 2 code base before and the team could not update to Rails 4 because they knew they would break.
Do you see how being careful can help? You called me paranoic, but I was always scared by the way Rails upgrades everything at once. If you want to upgrade ActiveMailer or ActionPack you're also forced to upgrade ActiveRecord, which has a terrible API and often introduced backwards incompatibilities. So, very soon, I decided to use Sequel instead of ActiveRecord and that with some other cautions allowed me to be always up to date with every new Rails version. I would often update to the newest Rails release in the exact day it was released and it would often just work after a few tweaks (in several cases with no tweaks at all). But I noticed how upgrading Rails was a nightmare to many people. But the way I choose what I want to depend on (I avoided any gems that would monkey patch some of the Rails libraries, for example) allowed me to have an easy time getting up to date. The reason why I moved from Rails had nothing to do with finding it hard to upgrade it. The upgrade process was always easy on our side.
It happens, projects get rewritten, dumped, completely refactored, you got to take action at some point.
The application I currently maintain was written with Grails when I got into this project. I slowly replaced it with Rails when working around some Grails bugs that were never fixed. At some point I moved completely to Rails. It wasn't a complete rewrite in the sense things were moved one little part at a time and trying to keep the logic when it made sense and improving over it as I saw fit. I would say I was evolving the application, not rewriting it. When I moved from Rails to Roda it was even simpler to move one part at a time to Roda as most of the code would remain unchanged as it's the same language. The only migration I had to perform at once was when I decided long ago to move the MySQL database to PostgreSQL since I can't move one table at a time ;) But the code itself required minimal changes in that case. The harder part was to script the data migration itself.
My point is that proper planning pays off.
6
u/Capaj Jun 17 '17
This is scaremongering. React integrates with javascript libraries just fine-the proof is in the ecosystem which features thousands of wrapper coomponents for even the most archaic JS libs. I've used on the projects a lot of them and they always worked well. If they did not it was a bug of that react wrapper or underlying ui library, not a bug in react.
BTW this shows how few people truly know how react works, because it got so many upvotes.
1
u/rrrosenfeld Jun 17 '17
The article never suggested it doesn't integrate. It was questioning whether this would always be the case or if that just happened by chance but the situation could change if the implementation details are changed. This has been already clarified in the React issue I created on GitHub and they explained what exactly they meant by "rerendering the whole app" in their documentation. I've updated the article to reflect that.
6
u/karathos Jun 17 '17 edited Jun 17 '17
Can you see the problem with that? If I can't really rely on the reconciliation algorithm to not touch the elements React is not supposed to manage, then I have no guarantees that it would be possible to integrate React with stateful third-party components in the future.
you have no guarantee with any piece of code that you don't own. future versions of that code could deprecate the public APIs you use and you'd just have to deal with it if you wanted to update. this has happened with virtually every software library.
you should be basing these decisions off how they handle deprecation, and so far, they've been very good with getting the word out and dumping all kinds of warnings everywhere.
-2
u/rrrosenfeld Jun 17 '17
That has not been my experience with jQuery even after several years using it and upgrading it. Or with Rails. When the library goes through a deprecation phase and then they remove little used public APIs and provide another way of achieving the same, this is way more manageable than relying on some behavior which the documentation clearly tells you you shouldn't count on. While I value React's concerns with regards to deprecations, no deprecation would be needed to transform shouldComponentUpdate in a hint since the documentation explicitly says we should consider it as a hint. That's the difference.
10
u/karathos Jun 17 '17
https://api.jquery.com/category/deprecated/
this is why you are accused of being sensationalist. you are advocating that, in general, adopting react.js seems risky because the documentation is worded in an unfriendly manner to your very specific use case. my rebuttal is, every single library does this. your counter argument is, "That has not been my experience". I can appreciate that, but it clearly does happen, and somebody was affected by these deprecations.
-6
u/rrrosenfeld Jun 17 '17
I expect every great library/framework to deprecate things and provide the new path to achieve the same. This has been the case with jQuery since I started using it several years ago. This was also the case with Rails for the many years I used it. However this is different in React because the documentation explicitly tells you that you shouldn't count on its implementation without telling you how to support third-party components, that's why I'm really concerned. React doesn't actually have to deprecate any changes to what it considers an internal implementation detail. Provided you're using public API from jQuery, Rails and most big projects, chances are that you're always be offered an alternative to the deprecated API methods as well as some time to migrate your code.
For that jQuery deprecated list, they have been deprecated long ago and I haven't been using them for a long time, but they always provided the new way to achieve the old results. This is what really matters to me. I just want to know how I am supposed to support third-party stateful components with React, and React documentation doesn't tell me. Or it tells me that I could use shouldComponentUpdate and rely on the diff algorithm and how it won't touch children from an empty container, and then it will warn you that shouldComponentUpdate could be considered just a hint in future versions and that we shouldn't rely on the diff algorithm. In other words, I don't know how exactly React expects me to integrate with third-party stateful components, that's my main concern.
2
u/digdic Jun 17 '17
na your article is crap lol... every popular long running lib/framework will deprecate things get over it
1
u/rrrosenfeld Jun 17 '17
You didn't get it. In this case there's nothing to be deprecated since React doesn't officially support any method to handle third party components.
2
10
u/s_tec Jun 16 '17 edited Jun 17 '17
I would like to point out that these concerns apply to any stateful React component, not just 3rd party ones.
For example, suppose I make a Counter component with up + down arrows and some local state. Then, I nest that deeply under some kind of router component. Depending on the URL, the router component will either render my Counter page or something else. What happens if I quickly change the URL, and then instantly change it back?
Intuitively, I would expect the count to be reset to 0, since the Counter component should be destroyed and then re-rendered (we left the page, after all). In reality, the count might stay the same because React batches the renders, and the diff algorithm notices that the tree still has a Counter component in the same spot. Thus, if React locks down their diff algorithm to prevent these glitches, they lose a lot of potential optimizations.
The good news (?) is that this affects everything, not just 3rd party components. There is no way on earth React would radically break their diff algorithm to the point where it randomly re-renders things - the sloppiness is so they can err in the opposite direction of doing too few renders. If their diff algorithm did stop tracking component identities one day, it would instantly break half the world (or at least the half that uses component state). I agree that more assurance would be nice, but I feel that the scale of brokenness this would create is assurance enough.
4
u/gaearon Jun 17 '17
Replied: https://github.com/facebook/react/issues/9926#issuecomment-309205972
TLDR: React makes some guarantees, but there is a tradeoff between progress and promising too much. In our experience with 30,000 components these guarantees are enough for apps, but YMMV.
-15
u/shad0proxy Jun 16 '17
Facebook patents are reason enough not to use React.
Google fucked everyone over who invested in Angular 1.
That leaves only vue.js. All aboard the vue train!
5
u/s_tec Jun 16 '17
Or use
preact+preact-compat, which gives you a React-compatible API under an MIT license. Vue is a nice library, of course, but hardly the only valid option!1
u/rrrosenfeld Jun 16 '17
I've been trying to get some hard guarantees from Preact as well:
https://github.com/developit/preact/issues/728
But until this bug is fixed, I'd have the same problem as I pointed in the article, that's why it's important for the reconciliation algorithm to have some hard constraints on how it should work:
-7
u/shad0proxy Jun 16 '17
Vue or bust! Vue is framework for the next 10 years.
4
u/rrrosenfeld Jun 16 '17
Vue looks a lot like Knockout.js which we have been using in parts of the application for some years. But just like Knockout.js, it seems harder to create components with Vue when compared to using JSX mixed with JS code when the HTML snippets are really small. It's easier for me to get the context with both the relevant HTML and code side by side. Also, the last time I benchmarked VDOM solutions were able to perform the initial rendering much faster than Knockout.js, which is important to me. I haven't checked Vue yet, but the results might be similar to each other.
-4
u/shad0proxy Jun 16 '17
Vuex is what separates it. That and if you want to write html instead of jsx.
1
u/CarpetFibers Jun 17 '17
How did people who invested in Angular 1 get fucked over? It didn't cease to exist. It still has an active community. No one is stopping you from using it.
63
u/BenjiSponge Jun 16 '17
TL;DR: Because React officially takes the stance that any component's
.renderfunction can be called whenever (which is why you're supposed to avoid side-effects in.renderfunctions), you can't guarantee that a stateful DOM element managed by something less "clean" than React won't just be re-rendered. So third-party code could be destroyed by a change in the React algorithm.I had a similar issue with this when I was considering using React on a site with ads. NFL created a component for this that relies on
shouldComponentUpdate, which /u/rrosenfeld notes is not a great solution.On a personal note, I think the conclusion drawn is a bit sensationalist. If you're making a library that relies on this, you're passing risk down to the users. If you're making an application that relies on this, there's nothing forcing you to upgrade React, breaking your app. React v15 will always have the same reconciliation algorithm, and it will always be compatible with all the libraries you currently rely on (at their current versions). Tons of long-term projects use very old versions of technologies.
Further, I'd give it a nearly 0% chance that React will upgrade the reconciliation algorithm to re-render more often than it does now, especially violating
shouldComponentUpdate. And even if they do, they will definitely include a new way to handle this. It's too common a usecase to just say "Well, you should have read the philosophy documentation; we're not given you a migration path".Granted... working third-party code into React is absolutely not fun. If your application is going to rely on a lot of vendor code mucking around with your DOM, you should probably skip it anyways.