DEV Community

Shreyaan Seth
Shreyaan Seth

Posted on

Why Do You Need a JavaScript Framework anyways?? How React, Vue, Can Save You Time and Effort

As a web developer, I know firsthand how challenging it can be to build a dynamic website using plain JavaScript. Without the right tools and techniques, it can be a struggle to keep your code organized, maintainable, and scalable. That’s why I’m a big fan of JavaScript frameworks like React, Vue, Angular and others — they provide a solid foundation and a standardized structure for your code, making it easier to build complex, dynamic web applications.

As a web developer, you might be wondering, “Why do I need a JavaScript framework at all? Can’t I just write all of my code from scratch using plain JavaScript?” While it’s certainly possible to build a web application without a framework, doing so can be quite difficult and time-consuming.

Consider, for example, a real-time chat application that needs to update the conversation in real time as new messages are sent and received. With plain JavaScript, you would need to write a lot of code to handle the websocket connections, track the state of the conversation, and update the page in real-time as new messages arrive. This can quickly become overwhelming, as you have to worry about managing all of the different components and interactions yourself.

You would need to write code to create and manage the WebSocket connections, as well as code to track the state of the conversation and update the page in real-time as new messages arrive. You would also need to write code to handle user interactions, such as sending and receiving messages, and code to handle any errors or edge cases that might arise. All of this code would need to be carefully organized and maintained to ensure that your application works correctly and efficiently.

In contrast, a JavaScript framework like React would provide a standardized structure for building this chat application. React would handle the WebSocket connections, state management, and real-time updates for you, allowing you to focus on the core functionality of your application. With React, you could write a chat application with just a few lines of code, using the framework’s standardized structure and built-in optimization techniques to create a performant, efficient application.

lets take another example: a simple to-do list app. With plain/ vanilla JavaScript, you would need to write a lot of code to create the app, including code to handle user interactions, track the state of the to-do list, and update the page in real-time as items are added and removed. Here’s a sample of what that code might look like:

const toDoList = [];
function addToDo(item) {
  toDoList.push(item);
  updatePage();
}
function removeToDo(index) {
  toDoList.splice(index, 1);
  updatePage();
}
function updatePage() {
  // code to update the page with the current to-do list
}
Enter fullscreen mode Exit fullscreen mode

As you can see, this code is relatively simple, but it would quickly become overwhelming as you add more features and functionality to your app. You would need to write code to handle user interactions, such as adding and removing items from the list, as well as code to update the page in real-time. This can quickly become difficult to manage, especially as your app grows and evolves.

But with a JavaScript framework like React, you can easily build this same to-do list app with just a few lines of code. React provides a standardized structure for your code, allowing you to break your application into small, reusable components that can be easily composed and updated as the state of your application changes. Here’s a sample of what that code might look like using React:

here’s a sample of what the to-do list app might look like using React:

import React, { useState } from 'react';
function ToDoList() {
  const [toDoList, setToDoList] = useState([]);

function addToDo(item) {
    setToDoList([...toDoList, item]);
  }
  function removeToDo(index) {
    setToDoList(toDoList.filter((_, i) => i !== index));
  }
  return (
    <div>
      {toDoList.map((item, index) => (
        <div key={item}>
          {item}
          <button onClick={() => removeToDo(index)}>Remove</button>
        </div>
      ))}
      <input
        type="text"
        placeholder="Add a to-do"
        onKeyDown={event => {
          if (event.key === 'Enter') {
            addToDo(event.target.value);
            event.target.value = '';
          }
        }}
      />
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

As you can see, using React allows you to build this to-do list app with just a few lines of code.

(the code may look bigger but its almost an complete app!!! unlike plain javascript which will do nothing)

Additionally React’s component-based approach would make it easy to break your application into small, reusable components that can be easily composed and updated as the state of your application changes. This can make your code more modular and easier to understand, which can save you time and effort in the long run.

In short, JavaScript frameworks like React, Vue, Angular and others can provide numerous benefits for web developers. They can save you time and effort, help you write better code, and make your application more performant and efficient. Whether you’re building a simple web page or a complex web application, a JavaScript framework can be a valuable tool in your toolkit.

Top comments (3)

Collapse
 
brense profile image
Rense Bakker

Most importantly, these frameworks handle DOM manipulation for you in an optimized way, because from the days of jQuery we learned that DOM manipulation becomes a serious performance problem in large (single page) applications.

Collapse
 
geomukkath profile image
Geo Mukkath

Vanilla can achieve the same with fewer lines of code. I've seen SMEs build static websites with React and Angular. Knowing when to use these frameworks is important. If you are building something simple like showcasing your business and details on a page then plain JS will do. In fact it will be much easier to develop.

For bigger applications (with a lot of dynamic content) , frameworks are a better choice.

Collapse
 
shreyaan profile image
Shreyaan Seth

i agree