If yes, why not use "vanilla" js?
If yes, why not use "vanilla" js?
Isn't that like asking why jQuery exists?
Personally I still believe that JavaScript and HTML should be separate, like HTML and CSS. I know that might make sound old, but I really dislike having a template language embedded in my JavaScript library, and as a result I end up disliking most of the newer frameworks.
I know jQuery, the documentation is good, it's easy to get help, and it makes sense to me in a way React, Angular and others frameworks do not.
Yeah, I could use plain JavaScript, I could also just use Python and not have a Django or Flask dependency. It's just easier and more productive to take the dependency.
Here's why IMO react is not actually mixing the two: HTML is just the serialized form of the DOM, your browser reads it, transforms it the a DOM tree before rendering it.
With react, you interact with the DOM (indirectly via a virtual DOM API), so you manipulate javascript objects, you only do javascript. JSX is just syntaxic sugar over that API, because deep nesting is easier to read with XMLish syntax. JSX is not a templating language (that outputs a string).
It helps me get the job done quickly for projects when I don't need/want a framework.
It is well documented, organized, and lively ecosystem of devs, documentation, support options, and -- yes -- plugins.
It is a consistent and easy to understand API, and the breaking changes are well understood between versions.
There are stuff like : File picker [2], dropdown [3], tip [4] for tooltips and many more.
1 : https://github.com/component
2 : https://github.com/component/file-picker
They're mainly for react, though other platforms have them (eg, I use 'ractive', from the guardian, which has its own component model).
Go check out npm, there are a while bunch of react components you can just pick up and use.
document.getElementById("that-element").addEventListener("change", function(e) {
// e.target.files
});
document.getElementById("that-element").click(); document.getElementById("that-element").click();
doesn't exist. You need to use something like /** Creates the click on the input */
const clickEvent = document.createEvent( "MouseEvents" );
clickEvent.initEvent( "click", true, false );
document.getElementById("that-element").dispatchEvent( clickEvent );
References :1 : https://developer.mozilla.org/en-US/docs/Web/API/EventTarget...
2 : http://stackoverflow.com/questions/6367339/trigger-a-button-...
EDIT ( sorry can't reply to you, because HN ) :
Relevant jQuery discussion
https://github.com/MediaCrush/MediaCrush/blob/master/scripts...
Here's the code in the jQuery library you linked to that does exactly this as well:
https://github.com/component/file-picker/blob/master/index.j...
The devil is in the details, although obviously your code works for any other clickable elements.
1 : https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement... 2 : http://stackoverflow.com/questions/210643/in-javascript-can-...
I use it for the same reason I use lodash when I could do everything it does in vanilla js: because it's a slightly more dev-friendly API.
Much like the PHP community, jQuery's community has earned it a reputation as a hacky tool for people who don't understand where jQuery ends and Javascript begins. However, it didn't become popular by accident, and in competent hands, it still has its benefits.
Then as I continued adding useful methods such as ajax() (no, in vanilla js it's not "solved") and turning events off (also non-trivial) I ended up with a jquery alternative:
It's not exactly the same, but most methods are the same or highly compatible. For example, the append() method is extended so this does what you'd expect it to do, generate a list with first, second and third items:
u('<ul>').append(text => `<li>${text}</li>`, ['first', 'second', 'third']);
Right - I use jQuery because I've greater confidence that `.ajax()` will behave correctly across a wide range of browser versions, though I don't have any good evidence about using that vs vanilla `XMLHttpRequest` these days. I'd be interested to know which bits of "not solved" - which aspects in particular are not yet well supported?
ajax('/api/users', function(){}, 'json');
(of course, if you want to know about compatibility, http://caniuse.com/#search=xmlhttprequest )
But the deficiencies of fetch() are another topic entirely, and I tend to use it (polyfilled tbh) for the majority of my async requests because I like the rest of its implementation.
> If yes, why not use "vanilla" js?
I can't answer that, but I can answer "Why not use $Framework?"
My current project (a large admin system) is crying out for the more complex parts of the admin UI to be built in React, Angular etc. The problem is it's an all-or-nothing situation.
When picking up a new technology I want to add a little bit to the current project, a bit more to the next, and so on. Progressive Enhancement for the developer. jQuery lets me do that; the frameworks don't.
I have used KnockoutJS in 2 locations in this project; both for a single component on a page which needed to be very dynamic. I have been impressed that it doesn't try and take over, and lets me think of enhancing the experience (and my skills) one component at a time.
We've been replacing certain parts of our vanilla js application with small React apps and it's worked brilliantly. There is a really good talk by Ryan Florence where he replaces backbone components (i think) with React components.
In any case this is an implementation problem. React base is around 30kb after min + gzip, and executing the code should take hundredth of seconds on page load unless you're on a really bad phone.
Maybe you've seen this behavior because of components designed to perform one or several high latency requests before they display data? If that's the case, then it's easily fixable by providing the initial data along with the page, or improve latency by many means.
Erm, because that's the impression I've got from the tutorials and demos. I guess a "Add $Framework to a bit of your app" isnt' sexy: the ones I've seen add their own routing and focus on SPAs. Rather than "Here's some incredibly complicated information to display, and we need to be fairly interactive over it (and how other data on the page affects it)".
Would it work if you didn't have a vanilla JS application but a traditional web app?
>Lots of people use React as the V in MVC. Since React makes no assumptions about the rest of your technology stack, it's easy to try it out on a small feature in an existing project.
- Nothing I've done is "big enough" to warrant a front-end framework - It's guaranteed cross-browser - There's a large ecosystems of plugins - The documentation is excellent - The API is clean, abstracts away some of the fiddliness of "vanilla" js
Why does anybody use a library? Because it has some good pre-writen code you can reuse. Vanilla js is verbose, full of extra control sequences and easy to get wrong.
something like $("#wrapperel").on("click",".painbutton",cbfunction),is way more code in plain js.
I haven't really used it in the last 4 years as most of my projects have had frameworks in place.
I definitely think it still has it's place - it is more concise and reads a lot nicer in many cases than vanilla JS for DOM manipulation and AJAX requests.
I know XML is not cool any more, but sometimes you don't have a choice.
Should you write a SaaS SPA in nothing but HTML and jQuery? Probably not.
Should you write the entire application in React just because you want to have a nice fade out effect on alerts and notifications? Definitely not.
2. Because jQuery provides a documented, backwards compatible, and stable API that works across multiple platforms.