?? How React Transformed DOM Manipulation: Side-by-Side Comparison
Yash Patil
Full Stack || Blockchain || Automation || Competitive Programming || Deloitte
Manipulating the DOM directly can feel tedious and repetitive, but with React, we handle updates in a cleaner, more intuitive way. Here’s a comparison of traditional DOM manipulation versus React to show just how much easier React makes the process! ??
1. ?? Updating Elements Dynamically
?? DOM Manipulation:
const button = document.querySelector('#myButton');
let count = 0;
button.addEventListener('click', () => {
count += 1;
document.querySelector('#counter').innerText = `Count: ${count}`;
});
?? React:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
Takeaway: React’s useState hook automatically updates the UI, sparing us from manually updating elements. ??
2. ?? Conditional Rendering
?? DOM Manipulation:
const container = document.querySelector('#greeting');
function updateGreeting(isLoggedIn) {
container.innerHTML = isLoggedIn ? '<h1>Welcome back!</h1>' : '<h1>Please sign in.</h1>';
}
?? React:
function Greeting({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
</div>
);
}
Takeaway: With React, conditional rendering is as easy as using if statements directly within JSX, no need for innerHTML or updating elements manually. ??
3. ?? Rendering Lists with Keys
?? DOM Manipulation:
const listContainer = document.querySelector('#todoList');
const todos = ['Task 1', 'Task 2', 'Task 3'];
listContainer.innerHTML = '';
todos.forEach(todo => {
const item = document.createElement('li');
item.textContent = todo;
listContainer.appendChild(item);
});
领英推荐
?? React:
function TodoList({ todos }) {
return (
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li>
))}
</ul>
);
}
Takeaway: React’s map with key props handles dynamic list updates gracefully, eliminating repetitive createElement and appendChild calls. ??
4. ?? Form Handling and Controlled Components
?? DOM Manipulation:
const input = document.querySelector('#nameInput');
const display = document.querySelector('#displayName');
input.addEventListener('input', (e) => {
display.innerText = `Hello, ${e.target.value}`;
});
?? React:
import React, { useState } from 'react';
function NameDisplay() {
const [name, setName] = useState('');
return (
<div>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
/>
<p>Hello, {name}</p>
</div>
);
}
Takeaway: React’s controlled components make form handling straightforward by syncing input values with state, removing the need for direct DOM event handling. ??
5. ?? Efficient Re-renders with Memoization
React’s use of memoization through React.memo optimizes re-renders in large applications, something not possible directly with traditional DOM manipulation.
const ExpensiveComponent = React.memo(({ data }) => {
console.log("Rendering...");
return <div>{data}</div>;
});
Using React.memo, only components with updated props re-render, enhancing performance and avoiding unnecessary DOM updates.
?? Final Thoughts
With React, the focus shifts from how to update the DOM to what the UI should look like, allowing developers to build intuitive interfaces without repetitive DOM handling. Try these approaches and see how React can simplify your workflow!
#ReactJS #WebDevelopment #FrontendDevelopment #JavaScript #DOMManipulation #Coding #TechTips #ReactTips #Programming #SoftwareEngineering #WebDesign #TechLearning #ReactComponents #DeveloperLife #CodeNewbie #CleanCode #ReactForBeginners #TechInnovations #CodeEfficiency #DigitalTransformation #FrontendEngineer
student at BML Munjal University
1 个月Very helpful!