React vs Vanilla JS - When to use what?

Aspiring Polyglot, learning and getting better each day.
Search for a command to run...

Aspiring Polyglot, learning and getting better each day.
No comments yet. Be the first to comment.
With the growing complexity of infrastructure deployments, security vulnerabilities can unavoidably creep into your Terraform configurations. In this blog post, we will explore how tfsec can help us identify and address security issues in Terraform c...

As the infrastructure grows in complexity, the need to deploy multiple instances of the same resource becomes imminent. Terraform offers two looping techniques, for_each and count, to tackle this. In this blog post, we explore how these techniques ca...

When I started out with Terraform, dynamic blocks were a tough nut for me to crack, atleast for a while. For some reason, reading the official documentation repeatedly didn't help my case. In this article, I have tried my best to explain the dynamic ...

Containerization made an entrance in the DevOps landscape when Docker was launched in 2013. Even though the fundamental concept of containerization existed before Docker, it was the first time people on a large scale were introduced to the idea of ru...

Website performance plays a huge role in enhancing the user experience of our websites. In this article, we will learn about performance optimization techniques like Debouncing and Throttling and the key difference between them. Debouncing and Thrott...

Web apps can be complex and may require a lot of dynamic functionalities. One can opt for Vanilla JS to build their applications but if you have worked with Vanilla JS before, you know how messy it can get. Here's when JS frameworks like, React, Angular and Vue, come into the picture.
In this article, I'll walk you through the main differences between a JS library like React and plain Javascript - when to choose which and why?

Let's start by answering two simple questions.
Vanilla JS is nothing but plain JS without any external libraries or frameworks. Using this we can build powerful and cross-platform applications.
React is a Javascript library used for building user interfaces. It allows us to make complex UIs from isolated pieces of code called "components".
React has quickly become one of the most popular Javascript libraries. This is entirely because of its flexibility and the improvement it brings in the performance. React breaks down the UI into smaller and reusable components that can move around data amongst each other. This breaking down of the UI is what gives React an edge over Vanilla JS.
In Vanilla JS, the code becomes very difficult to maintain if the application is large because in such cases the UI needs to be updated regularly. Here, to change a UI element you need to first find the element in the DOM and then update it. This is fine when you have to update only a single element but imagine doing this on long-form which a user needs to fill. This could be very memory and browser intensive.
This is where React comes in with a great feature i.e its own virtual DOM. The virtual DOM is a shortcut to bypass the manual work. It is a lightweight copy of the actual DOM. It has the same properties as the real DOM but lacks the power to make changes on the screen.
The most important and fundamental reason why modern frameworks are used is that, with Vanilla JS, keeping the UI in sync with the state is hard.
Consider Facebook. Say at a given time, your friends comment on a picture of yours and you want to see it immediately. You'd want to shift from liking posts to commenting or sharing without being slowed down.
Now, this can be done in Vanilla JS too, but the number of changes you'd have to do in the code would be very tedious. All this tiresome work can be avoided by using something like React.
Basically, with React, we can manage to keep the UI and the state synchronized with each other. In Vanilla JS, if you have to change the state, you would need to update the UI. One small mistake and your UI could be out of sync with your data.
Code organization and re-use is another important aspect of React. A component created only once can be used multiple times with different data.
Whether you should use Vanilla JS or React depends very much on your use case.
Vanilla JS is awesome but it's not a great alternative when it comes to building huge applications with complex dynamic functionalities. Besides, it cannot create complex and efficient UIs. So if you have an app that changes frequently and drastically with thousands of pages, it is better to use a modern Javascript framework.
On the other hand, React which allows us to use reusable components and is capable of keeping the UI in sync with the state, can definitely solve this problem.
If you liked this post, consider subscribing to my newsletter.
Thanks for reading!