How React Works Under the Hood: Building a Tiny React Clone with Vanilla JS
Ever wondered what it would be like to build a React app without React? đ§ Well, brace yourself because weâre about to dive into the chaotic yet enlightening world of manual state management and DOM updates with vanilla JavaScript. By the end of this, you'll not only understand React better but also appreciate why you don't write apps like this anymore. đ
The Problem: Why React Exists
Picture this: Youâve got a simple button that displays a count. Every time you click it, the count increments. Sounds easy, right? But hereâs the twistâyou canât use React, Vue, or any of those fancy frameworks. Just plain old JavaScript.
Now, without Reactâs magic, youâll quickly realize that keeping the UI in sync with the state is like juggling chainsaws while riding a unicycle. Let me show you what I mean.
The Manual Approach: Vanilla JS FTW đ±
Hereâs the code that simulates Reactâs state management and rendering process manually:
let state = {
count: 0,
};
function onButtonClick() {
state.count++;
console.log("onButtonClick is called");
buttonComponentRerender();
}
function buttonComponentRerender() {
const root = document.getElementById("root");
root.innerHTML = ""; // Clear the root element
const component = buttonComponent(state.count);
root.appendChild(component); // Append the updated component
}
function buttonComponent(count) {
const btn = document.createElement("button");
btn.addEventListener("click", onButtonClick); // Attach the click handler properly
btn.innerHTML = "count " + count;
console.log("button is created");
return btn;
}
// Initial render
buttonComponentRerender();
Breaking It Down: Whatâs Happening Here?
1. State Management
The state
object holds our applicationâs dataâin this case, just a count
property. Every time you click the button, state.count++
updates the count. Pretty basic, right?
2. Event Handling
The onButtonClick
function handles the buttonâs click
event. After updating the state, it triggers a rerender by calling buttonComponentRerender()
.
3. Manual DOM Updates
buttonComponentRerender()
:
Clears out the
root
element usingroot.innerHTML = ""
.Creates a new button by calling
buttonComponent()
.Appends the updated button to the DOM.
4. Dynamic Component Creation
buttonComponent(count)
generates a new button element with the updated count and attaches the click event listener. Every click rebuilds the button from scratch.
The Pain Points: Why This Sucks đ
Performance Nightmare: Weâre nuking the entire DOM and rebuilding it on every click. Imagine doing this for a complex UIâyour browser might just give up on life.
Messy Event Handling: Reattaching event listeners on every render is tedious and error-prone.
No Reusability: Every component is manually created, so scaling this approach is like writing essays by carving them on stone tablets.
The React Way: Why React Is a Blessing
React simplifies this chaos by:
Virtual DOM: Instead of nuking the entire DOM, React calculates the minimum changes needed and updates the DOM efficiently.
Declarative UI: With JSX, you describe what the UI should look like, and React handles the how.
Reusable Components: Components can be reused like LEGO bricks, making your code DRY and scalable.
Hereâs what our button component would look like in React:
import React, { useState } from 'react';
function ButtonComponent() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
count {count}
</button>
);
}
export default ButtonComponent;
What We Learned: Manual vs React
Building this tiny React clone taught us a few things:
State and UI are closely linked: Any state change requires a UI update.
Frameworks save time and headaches: React abstracts away the nitty-gritty of manual DOM manipulation.
Appreciate the tools you have: Writing apps like this manually is a painâReactâs declarative model is a lifesaver.
Final Thoughts: The React Love Letter đ
If youâve ever taken React for granted, try building something manually with vanilla JS. Itâll make you want to hug Reactâs Virtual DOM and say, âIâll never doubt you again.â
Now, go forth and write some React appsâand the next time someone asks why youâre using React, youâll have the perfect answer.
P.S. If this blog gave you flashbacks to early 2010s web dev struggles, youâre not alone. Drop a comment or meme below to share the pain! đ€Ł