174 points · by ziodave · 9y ago · 49 comments
I'm being a bit snide here since the Java community had to endure years of uninformed ridicule regarding the meta gymanstics required for component oriented programming.
I remember talking to a react or backbone (or whatever, it was the current 'thing') developer 1.5 years ago and giving a brief history of GUI toolkit frameworks before browsers. I swear, his eyes lit up like I was spilling occult secrets. Back to the future, par per course in this forgetful field.
[p.s. it's not just the browser side people. Many years ago I mentioned in a Go community group to take a look at the Servlet architecture (circa ~2000) and its minimal but powerful abstractions. I think it was yesterday that we had people cheering "context" in Go 1.8.]
I would love a brief history of GUI programming concepts and patterns if you could point to one.
Fowler has a good overview of GUI architectures: https://martinfowler.com/eaaDev/uiArchs.html#HumbleView - note that this is at this point dated and does not cover the 'reactive' model.
He also recommends this survey: http://aspiringcraftsman.com/2007/08/25/interactive-applicat... (Haven't read it myself.)
Java also used to have a funky toy like thing in the beginning called Bongo (by a company called Marimba). It was a bit of a toy, but I found it an interesting, if flawed, take on scriptable GUIs. This is all I can dig up today: https://people.apache.org/~jim/NewArchitect/webtech/1997/10/... (If anyone here knows where I can download Bongo I'd hugely appreciate it. It was really fun to play with.)
I haven't seen anything about 'design-surface' & 'design-time' in context of Javascript frameworks yet but I have a feeling that will happen in due course. Can't dig up anything that is not .NET specific but it is a generally applicable idea for component based systems. Think GUI builders in visual studio or NetBeans. This is more of interest if you want to create your own framework.
Component models are a huge topic.
In the beginning (NATO, 1968): http://homepages.cs.ncl.ac.uk/brian.randell/NATO/nato1968.PD...
Hope this is of use to you.
A quick search turned up this list of several tools along that line: https://github.com/xyc/til/tree/master/react/devtools
On the other hand, you get to meet people all over the industry who were asked to deliver a simple Frontend app but built a new trending framework not because they had to solve a nontrivial problem but because they projected too much into the future that they thought that introducing some meta gymnastics will solve a probable UX scenario in the next 2-3 years down the product line!!!
Vue certainly has its own cruft, but it's at least somewhat simpler when it comes to how components compose.
That said, it _is_ a very useful pattern, and can help with conceptual management of components. I have links to a number of additional articles discussing this pattern as part of my React/Redux links list ([2]).
[0] https://medium.com/@dan_abramov/smart-and-dumb-components-7c...
[1] https://twitter.com/dan_abramov/status/802569801906475008
[2] https://github.com/markerikson/react-redux-links/blob/master...
React itself is actually a pretty simple library. The problem is that it's not the whole story, so you need other bits and pieces to put together a functional SPA. Ender redux, react-router, etc. And because React tries to explicitly not be so opinionated about what you should use, the way you learn about this stuff is not from the official tutorial, but from various blog posts.
There are also resources like this: https://github.com/kriasoft/react-starter-kit. On the surface it's great. But for my taste it's organized very poorly. You have all your reusable (NB: reusable != reused) components in one place, and all your pages in another. Each component consists of exactly 3 files that are 3 directory levels down. Pages (views? containers?) are in a separate directory structure where each page consists of 3 files that are 3 directory levels down. I guess if your SPA consists of hundreds of pages/views/containers with most components reused many times this makes sense. For my applications (low dozens of pages/views/containers) this seems like a huge overkill.
And don't get me started on the redux theory of all global state all the time, which actually seems to not be ideal for things like "I just want a more complex <select>", and you have a very scattered learning curve.
By contrast, Vue has a pretty sane slightly opinionated learning curve. Sure, there are a few things you need to figure out (XHR/Websockets + VueX + promises don't have a decent recipe in the docs), but overall it's much more coherent.
You should use whatever file structure works best for you. "Folder-by-file-type" is common, as is "Folder-by-feature". My links list has a category discussing various project structures ([0]), but that's completely up to you.
Redux's global state has tradeoffs. There's a lot of value in keeping your data outside the component tree (passing data between components that aren't directly related, making hot reloading easier, etc), as well as benefits to having a lot of logic be centralized (consistent handling of API responses, logging of dispatched actions). The tradeoff is that some aspects do become more complicated.
However, it's also important to note that YOU ARE NOT REQUIRED TO KEEP EVERY LAST BIT OF DATA IN REDUX!!!!! People have over-interpreted the phrase "single source of truth", and game-of-telephoned that to mean "You _must_ keep _all_ data in Redux". No. NOT true. Per the relevant Redux FAQ entry ([1]):
> Using local component state is fine. As a developer, it is your job to determine what kinds of state make up your application, and where each piece of state should live. Find a balance that works for you, and go with it.
So, if it makes sense to keep a bunch of data for a <select> in the component that's rendering it, do so.
Finally, that "bits and pieces of info" aspect is exactly why I've put together my links list: to try to provide a single place that points a reasonably vetted selection of good info on React/Redux-related topics.
[0] https://github.com/markerikson/react-redux-links/blob/master...
[1] http://redux.js.org/docs/faq/OrganizingState.html#organizing...
Google confirms this expression was coined here. Thank you!
I was annoyed by Javascript community churning out a new tool every week and deprecating everything every year. I lived through learning Ember and Angular1. Now, I just picked up Vue, a few crucial components from Vue MDL and wrote everything else in plain Javascript, CSS and a little bit Jquery. And just included scripts in my html. That's it - you get two innovations of Javascript world that make the biggest difference, two way data binding and components, at the least possible cost. It saves you from all the madness of https://hackernoon.com/how-it-feels-to-learn-javascript-in-2... , half of which will be likely deprecated in a year anyway.
This workflow is likely more productive for 95% of sites, unless you are relaly building a complex single page web app. It will be more resilient to the passage of time.
https://medium.com/the-vue-point/vue-in-2016-8df71d98bfb3#.u...
HOC aren't to bad to know either, if you want to end up with maintainable code.
shameless plug: https://github.com/kay-is/react-from-zero
It feels like there's more risk in making the wrong choice. I see lots of regrets around redux, for example, adding more complexity than initially wanted. Or, selecting a tool that becomes abandonware, etc.
In 2015 we had countless Flux implementations and most of them have been abandoned. I did an app with Flummox, for example and switched to Redux later.
But now? Redux and MobX are basically the major players and you can still do small to medium sized apps without them.
How are events in React supposed to be propagated from low level components to their parent components when the low level components should not have any knowledge of their parents?
There's a great article that describes various component communication patterns at http://andrewhfarmer.com/component-communication/ .
Basically, you don't propagate directly to your parent. In React there should be only one source of truth. So in your case, the event in a low level component should emit an action that will modify data in your Store/Container/Model. Then this change will be passed down (with the correct setup) as props to all of your components.
>React best practises question - At what level should event handlers be installed? At lowest level components or the highest level?
I personnally prefer when the handlers are close to the associated JSX. I doubt there is any requirement on this other than maintainability.
He was asking for best practices, certainly a sizable amount of developers would argue to use a flux implementation for that particular problem, as a best practice.
Something about this makes me want to facepalm and wonder why we bothered with separation of concerns, when React just undoes all of that.
But as I've got more used to it, I'm not sure it's really an issue, or at least doesn't have to be.
Firstly, JSX isn't really HTML. It's a bit of syntactic sugar around JavaScript "createElement" functions. If you don't want to have something that looks similar to HTML in your JS, you don't have to.
Secondly, if you want dynamically generated HTML, you're going to need some form of logic embedded in there somewhere (loops, conditionals etc). Most template languages end up with their own directives, like Angular 2's "ngFor". The React answer is to use standard JavaScript for that control.
And if you then want to separate out the non-display logic, you can use a combination of "smart" (pure JS logic) and "dumb" (JS/JSX display) components, and treat the JSX files as templates. "Separation of concerns" in this context isn't about not mixing code and markup, it's about not mixing business and display logic, and there's nothing in React that stops you doing that.