Boiling React Down to Few Lines in JQuery
hackflow.com
hackflow.com
Example:
function MyComponent(element) {
var instance = this;
instance.element = element;
instance.history = [];
instance.state = { myKey: 'someData' };
instance.template = _.template($('my-template').html());
instance.setupHandlers();
}
MyComponent.prototype.render = function() {
var instance = this;
instance.element.html(instance.template(instance.state));
instance.history.push(deepcopy(instance.state));
}
MyComponent.prototype.undo = function() {
var instance = this;
instance.state = instance.history.pop();
instance.render();
}
MyComponent.prototype.setupHandlers = function() {
instance.element.on('change', '.my-input', function(e) {
instance.state.myInput = $(this).val();
instance.render();
});
}
This is of course a very simplified version but it should give you an idea of what I'm talking about.There are other issues such as event delegation,cleaning up event listeners,... that will make your solution hard to scale past simple widgets.And before you know it,you'll be writing your own complicated framework that does less than Angular or React.
I think the jury is still out on whether storing all application state in a single state object (as in Elm) scales well beyond toy examples. All the state is exposed which doesn't seem so good from a data-hiding point of view.
1. For each domain, state can be modified in one place (in React, a store).
2. For each domain, state can be read in once place (a Presenter).
This preserves data-hiding for the most part, and keeps you from having to play hunt-the-wumpus when you need to change the data structure.
I'm not familiar with Elm, but what do you mean by this? Even if all the application state isn't literally in a JS object like {users: [...], messages: [...], ...}, it's going to be in multiple variables like UserStore and MessageStore. There's really not a big difference other than the syntax of accessing them. But perhaps you're hinting at a drastically different approach to application state.
The issue is that you can't really write UI components as you'd normally think of them; everything needs to be divided up into a separate models and view functions. The state of every single widget in the page (recursively) needs to be represented somewhere in the application model.
I am fully aware of this issue, again, this is a stop-gap between the jQuery mess we had and proper framework. This is not being used to run the whole front-end, it's only being used for components that are loaded on a page. And in fact the one page that we have that does a full re-draw on every change is actually extremely fast, you don't even notice it. Again, this is not to say it's the end goal, just a step on the staircase to a JS framework. Just getting away from building a component in PHP then modifying it in JS has been a huge win. My rule of thumb is logic should only be implemented in 1 language and so if you need to update/modify anything on the client side (which we almost always need to do, this is a Web App and not a website with some JS sprinkled in, even if that's how it started) you need to do it all in JS or else you get into a case all too easily where you edit the JS or PHP and not the other. Not to mention that if we wanted in the future we can alway render the first load on the server (using JS) and then subsequent renders could happen on the client with the same code.
It's also a philosophy of UI composition. It's also a philosophy of data consumption. It's also a philosophy of code structure.
Having written my share of code with both, I now prefer composing UI's in React. It's easier to get simpler code maintenance and better performance with React.
There are also ideas beyond React I left aside. I will probably write some follow up post later.
I don't think this is good advice. It would be better to just start by using something like React, then if things get too slow, implement shouldComponentUpdate, because you will almost certainly need something that can make efficient DOM updates instead of just naively recreating whole DOM trees.
Great piece though. It's good for programmers to understand how powerful the concept can be of factoring state out of actions of your app.
I still use jQuery.
All these frameworks make you write more code than needed... and because people started to feel it wasn't worth it, their latest pitch focuses on speed. They say the "virtual DOM" is the future, and if you're not diffing your state to re-render the DOM you're not a serious developer.
I still don't see the benefit. I've written 15k lines of javascript code in some apps. My components have state and I re-render as needed, and as long as you separate code between components it's fine.
How many times have you been using a js app and thought "wow this DOM is slow?" You don't. It's either 0.1 sec or 0.2 sec to re-render the whole component/widget. 99.9% of the time a website is slow because the server requests take too long. Every once in a while you'll see some crazy CSS3 animations that make things unresponsive.
The people who go on about having a virtual DOM run benchmarks on thousands of elements... I don't know about you, but I'm never rendering thousands of elements at a time. Ajax/pagination works fine for extra data. I did write a very simple template system and event management system... but I'd rather do that than add the complexity of a framework. Dealing with a framework to make things work in "the X way" is wasting everyone's time. And then the next Angular or whatever comes along... and everyone wastes more time learning the latest version. This has turned into a rant... but I just want to say I'm happy as a coder who avoids frameworks at all costs. At the same time I try to use as many libraries as possible. Libraries save time, frameworks waste time.
I've yet to run into any problem that were supposed to happen using jQuery and vanilla Javascript.
Even outside of Javascript, I stay away from using full blown frameworks. Even libraries that you think will be perfect for your use case turns out to be a drainage of resource learning, and now being bent to the will of the author's opinions. For example, Celery is a complete piece of shit. The amount of bug, and workarounds that one must experience vs. writing something on your own using RabbitMQ or even just redis, it's clear to me. Same with giant PHP frameworks or RoR vs. Flask. Even microframeworks that focus on not being a framework comes with the some opportunity cost, however being better than the monolithic frameworks that whines and decides to leave you in the dark because you did not share the same opinions.
Yup, just one single .js file written in jQuery. I am however, curious about React.js, and want to experiment with new components, I doubt I would use Backbone.js however.
It took far longer, and more bugs using Backbone.js than using jQuery. I don't know why people are so obsessed with Backbone.js, modularizing, object orientation with Javascript, the language was not optimized for such purposes, it's only with the server side javascript boom with node.js that we are seeing this heavy shift towards Javascript, but I treat Javascript like the second class citizen it really is, and I think it's rather naive to suddenly start using it for everything just because you can and everyone is doing it.
* Is it an application (google docs?), if not, you may not need a framework.
* How large is the team? For a solo project, a framework offers less of a consistency benefit. In a group, having people implement the same way can be important, and a framework helps a lot.
* Are you managing a lot of application state in javascript?
I'm out of time, but those are some of the big ones. I've used frameworks in the past, but we're currently doing a classic wizard-style application, and the frameworks don't offer us a lot. If it was less site & more application, I would be pushing for one of the popular frameworks to help manage complexity. Frameworks bring additional complexity, and they need to mitigate a certain amount of complexity to be worth it.