Live data from Hacker News

Why you might not need MVC with React.js

code-experience.com

31–40 of 81 posts

Re: Why you might not need MVC with React.js

#31
I've never been a fan of MVC (I've used it for both php/backbone.js and iOS). My main problem with it is that the functionality of model is obvious, view is obvious, but controller can be anything from "a model for multiple views" to some kind of delegate, to an extension of the model. I find it so nebulous that even today, I can't explain to someone with 100% confidence where the model ends and the controller begins in their application.

Contrast that with the front end/back end metaphor of the early internet, which was very easy for people to understand. It perfectly matched the way they had interacted with their bank, or command line tools, or even television. Hard to get much simpler than that!

I think where MVC went wrong is that it tried to bring a portion of the back end (the model) into the front end (the view) and call it a “controller” without effectively explaining what that meant. To me, backbone.js seemed like mainly a cache of the server. As in, there are a multitude of other ways to do what it does, from XMLHttpRequest, to a nested iframe, to just manually writing a bunch of javascript that encodes the controller. I found that it simplified the easy work of translating from a rest API to objects, but did almost nothing for the hard work of choreographing how all the parts worked together. I never got far with Angular.js but my impression of it is that it replaces one complex choreography for another.

My hunch is that the solution to all of this is a better data-object mapping that removes most of the code and works more like a graph, the way MS Access or Filemaker do, since most of the web is just CRUD apps anyway. The message passing of objective-c is an ok way to accomplish this, but it needs to be generalized and will probably work more like the Actor model:

https://en.wikipedia.org/wiki/Actor_model

So MVC will be replaced with automatons that run in their own sandboxes and interact with each other through explicitly specified channels, the way that Go works. A high level example of this is that you wouldn’t encode a select box to be enabled/disabled based on a boolean in some JSON, but that the select box would simply be inaccessible for certain data because its container actor would be in another state. If you think about this, it mirrors how unix works, with a set of very simple functions and permissions that dictate what can do what. I was first exposed to this style of programming in HyperCard back in the 80s, then later in Flash and Unity. One thing I would change about it today though is to remove the notion of time so that it works like MS Excel or functional programming, so that all of the states can be exercised and unit tests basically become an enumeration of all the states you want the program to arrive at.

Re: Why you might not need MVC with React.js

#32
MVC and Object Oriented designs have consistently been misapplied to various runtimes since their introduction and proper use in systems like Smalltalk and Self (I'm like a broken record if you know me at all).

The MVC patterns in Ember and Angular have never made any sense.

The only good thing about Angular are the directives. They made a big mistake in adding Services, Controllers, Filters, Routes and all of these super opinionated, bulky, slow things that aren't always needed.

Ember suffers from similar bulk but the DOM/data binding is nowhere as elegant as Angular or React.

Another great data-binding abstraction is d3. d3 is great for working with and displaying large data sets. It also does the right thing and keeps it's functionality simple. It's not trying to be the only abstraction in your tool belt.

I've been using a home-grown abstraction for data retrieval and test mocks that has be loosely inspired by Angular's Services, but is much more functional in design. I'll release it in a few months once it has survived a few more iterations on this product I'm working on. It's called getIt. getIt and React. Get it?

Every time someone sits down and decides to build THE ONE SYSTEM TO RULE THEM ALL... it fucks up... Mordor, the Soviet Union, Angular, whatever, same rule applies. :)

Re: Why you might not need MVC with React.js

#33
post #12
post #9

Well you need a router and a way to persist data.Does React offer this? How do I manage page changes with React? React only concern is the view afaik. An app is more than a view,even in the client. So react is not MVC,but you still need MVC somewhere.unless you write one page apps of reactive documents. > It does not define how communication flows. I think it does ,in pure MVC: - view is stateless,view is 100% model…

React doesn't offer routing. The React TodoMVC implementation uses Director, a separate library for routing: https://github.com/tastejs/todomvc/tree/gh-pages/architectur...

You can also use something like react-router-component which integrates very easily with React:

https://github.com/andreypopp/react-router-component

Re: Why you might not need MVC with React.js

#34
There are two problems with React which I see at the moment: 1) Layouting 2) (more to the point of this article) what if your app isn't composed as a tree? More specifically, what if I have two tightly coupled components in two different places (physically and hence in the DOM), which need to share state? I don't want to have to go through a massive tree all the way to the root just to get that information between the two components work.

I also want components which are reusable and some of this information flow make tight coupling between parents and children. There are certainly things to be figured out, React is no silver bullet.

Re: Why you might not need MVC with React.js

#35

I've never been a fan of MVC (I've used it for both php/backbone.js and iOS). My main problem with it is that the functionality of model is obvious, view is obvious, but controller can be anything from "a model for multiple views" to some kind of delegate, to an extension of the model. I find it so nebulous that even today, I can't explain to someone with 100% confidence where the model ends and the controller begins…

That's funny, because in my opinion, the functionality of the model, data without display logic, is obvious, and the view is obvious, layout for data. but the line between the controller and the view seems very nebulous. At which point is the data general enough to warrant a new view, or specific enough to need only a controller to marshal between the view and the data... It seems to me that a Model->Controller->Widget is a much more strong distinction, where Widgets are simply things offered via the toolkit, or controller functionality that is made general once you find yourself wanting to use similar render for multiple pieces of data.

Re: Why you might not need MVC with React.js

#36
post #34

There are two problems with React which I see at the moment: 1) Layouting 2) (more to the point of this article) what if your app isn't composed as a tree? More specifically, what if I have two tightly coupled components in two different places (physically and hence in the DOM), which need to share state? I don't want to have to go through a massive tree all the way to the root just to get that information between th…

In a large Backbone project, I experienced this issue.

Many people solve the problem with an event bus. To me, that makes debugging a huge pain and coupling rather difficult to analyze.

I solved it by passing a single flat context object that provides the API exposed by all of the ancestors of a component. Components can either pass this context unmodified to their children or make modified copies to add to the API seen by their children.

The advantage here is that intermediate layers of the tree don't need to know about APIs they don't directly consume, solving the problem of making every intermediary component mediate communication between parent and child.

As for the somewhat more difficult problem of allowing distant parts of the app to communicate (say, A and B), the key is abstracting objects for holding application state and communicating changes across the app into a new component C, which must be held by a common ancestor D of A and B. D then becomes responsible for mixing C's interface into the context object received by all its children. Typically D should be a controller-type component, mostly existing to serve as the nexus for a subsection of the app. D might also be the top level app object. But partitioning concerns into separate components like C avoids bloating D, and yields a lot of flexibility.

Other interesting patterns can be done as well. Optional hooks can be defined as context methods. The flat namespace of the context object also acts like a facade, decoupling the consumer of API methods from the actual provider. By making every member of the context a function (as opposed to data members), late coupling is achieved, which I found very useful in simplifying initialization dependencies.

On the whole, I found the flat context to be extremely flexible, and developed a number of patterns for solving particular problems. I have an unfinished blog post on the topic that I may one day get around to.

Re: Why you might not need MVC with React.js

#37

I've never been a fan of MVC (I've used it for both php/backbone.js and iOS). My main problem with it is that the functionality of model is obvious, view is obvious, but controller can be anything from "a model for multiple views" to some kind of delegate, to an extension of the model. I find it so nebulous that even today, I can't explain to someone with 100% confidence where the model ends and the controller begins…

it's worth reading some of the early smalltalk/MVC stuff to try to understand how MVC was originally formulated (http://www.ics.uci.edu/~redmiles/ics227-SQ04/papers/KrasnerP...). it can be a little hard to understand, because it's pretty significantly different from how most modern GUI toolkits work, but (as i understand it, not actually having written smalltalk code) at its core it's fairly simple:

Model: business data

View: on-screen representation of Model; reacts to changes in Model to re-draw accordingly

Controller: maps user input to changes in Model; may need to interact with Views, and may pass control through a hierarchy of Controllers, to determine the Model being manipulated

in other words, user-input data flows as: C -> M, and M -> V. i see a Controller modifying a View (rather than just querying it) as impure MVC, and i normally strive to avoid it, even though that's essentially impossible if you're forced to use a platform's GUI toolkit.

i don't think of MVC as having "gone wrong," but rather that the name has been co-opted and the pattern corrupted by so many different people, in many different ways, as to make the original name extremely confusing. iOS' concept of "MVC" drives me crazy. i think it's probably fair to call it MVP (Model-View-Presenter), the difference being that the Presenter can indeed do some of the work of modifying the View. but i still mostly think of this as a hack: the more work that gets shoved into the Presenter, the more complicated it gets.

Re: Why you might not need MVC with React.js

#39
post #27

Earlier quoted context omitted.

That has less to do with React specifically, and more to do with how languages/tools/libraries generally gain adoption. React is at a point where there's still an emerging "early-adopter" community. Angular was like that a year ago (I distinctly recall the tone on HN to be similar to your impression of the current sentiment around React). Now Angular is starting to reach the mainstream (loosely defined), which means…

I don't know, I think the excitement around angular was because it made things easy, while the excitement around react is based on its simplicity of its model. It's a deeper thing than just being a shiny new thing imo.

I wish I could see where you're coming from, but it doesn't seem simpler to me as compared to angular, just different. I need to read through the code for React like I did for Angular.

Re: Why you might not need MVC with React.js

#40
Um, ChaplinJS (an MVC framework built on backbone) http://chaplinjs.org has done this sort of thing for two years. You have views and subviews, and you listen for events with @delegate (which maps to $.on using view's container) in each of those views. You almost never need to use global events, in fact it discourages their use.
Post reply on HN