Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React is bloated, but Vanilla isn't the only solution.
Harsh Singh
Harsh Singh

Posted on • Edited on

     

React is bloated, but Vanilla isn't the only solution.

React is bloated, but going complete Vanilla JS can make your app really difficult to maintain and it severally impacts the DX (Developer Experience).
Just think about writingdocument.querySelector('...') everytime you want to fetch an element from the dom or writing$element.addEventListener(...) everytime you want to attach an event to an element.

I am not an anti-vanillaJS or anti-framework guy but trust me there's a mid-way! that is using a tiny toolkit which acts as a very thin superficial layer over the Vanilla JS ( toppings?? ) but still not something as bloated as a library or a framework. Just imagine if you could replace all thosequerySelector(...) calls withqs(...) orqsa(...) forquerySelectorAll or thoseaddEventListener(...) calls with some JQuery-likeon(...) andoff(...) functions just by adding a tiny toolkit at the top of your app.js file.

I know that a lot of you already use some sort of Javascript toolkits you made for personal use and have even used in client projects but a lot of newcomers think you either have to go completely the framework way or you have to completely go the vanilla js way. but it's a wrong thinking.

To prove my point I am gonna show you the tiny toolkit that I use in my projects, it's calledtez.js ( tez means fast in Hindi ).

/* tez.js | Created By Harsh Singh | MIT License */constdoc=document;exportconstqs=(sel,ctx=doc)=>ctx.querySelector(sel);exportconstqsa=(sel,ctx=doc)=>ctx.querySelectorAll(sel);exportconststyle=(el,obj)=>Object.assign(el.style,obj);exportconstattr=(el,name,val)=>{if(val){returnel.setAttribute(name,val);}returnel.getAttribute(name);};exportconston=(el,evt,hand)=>el.addEventListener(evt,hand,false);exportconstoff=(el,evt,hand)=>el.removeEventListener(evt,hand,false);exportconstready=hand=>{if(/complete|loaded|interactive/.test(doc.readyState)&&doc.body){setTimeout(hand,1);}else{on(doc,'DOMContentLoaded',hand);}};
Enter fullscreen modeExit fullscreen mode

this tiny javascript toolkit will save you from the pain of going complete vanilla-js way but will also save you from using a framework.

here's a tiny counter app i made with this toolkit.

<divid="counter"></div><buttonid="inc">+</button><buttonid="dec">-</button><scripttype="module"src="/app.js"></script>
Enter fullscreen modeExit fullscreen mode
import{ready,qs,on}from'/tez.js';constinitApp=()=>{constcounterEl=qs('#counter'),incBtn=qs('#inc'),decBtn=qs('#dec');letcount=0;// initial stateconstupdateCounter=v=>counterEl.textContent=v;updateCounter(count);on(incBtn,'click',()=>updateCounter(++count));on(decBtn,'click',()=>updateCounter(count===0?0:--count));};ready(initApp);// initializes the app when the dom is ready.
Enter fullscreen modeExit fullscreen mode

you can clearly see that even a tiny toolkit can improve your DX drastically without costing your app the performance benefits of vanilla js.

JaiHind #JaiShreeRam

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A self-taught Web developer and aspiring Pixel artist.
  • Location
    INDIA
  • Work
    Web developer, Indie Game Developer at self employed
  • Joined

More fromHarsh Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp