Introduction to React.js
Why one should learn react.js?
There are 2 good reasons:
1. Virtual DOM (Also known as React DOM):
Let me try to explain in detail:
What is DOM:-
- Standardised API that represents an HTML, XHTML, or XML document as a tree structure.
- Each node on the tree is an object representing an HTML element.
- Connects JavaScript to HTML.
- Dom nodes can be manipulated directly eg.(using document.getElementById).
BUT DOM IS A MESS. WHY?
- Actually, The DOM itself is not slow, however, when you make changes that trigger re-painting of the layout, that’s where things start to slow down (especially if you do a lot of manipulation at once).
- Let us understand the web workflow.
- The dom tree itself is constructed by the rendering i.e from parsing the HTML document.
- Parse CSS and applies CSS to the HTML creating a render tree.
- Render tree is painted to the browser.
- Now making changes is expensive as all the nodes have to go through these things.
Now here comes the Virtual Dom:
- Instead of rendering all these changes to the real dom, we apply them first to the virtual dom, virtual dom doesn’t get rendered in real life so changes are cheap.
- Batch changes together for efficiency.
- It’s like editing a blueprint then creating an entire building every time we do any changes.
What is virtual dom?
- A lightweight representation of dom.
2. Exists in memory and is never actually rendered.
3. It’s just a tree data structure of plain javascript objects.
4. Popularised by react.js (also used by Angular and Vue).
2. Reusable and clearer web components:-
I highly recommend any Web developer to have at least a basic understanding of React.
That’s because for a few reasons.
1. React is very popular. As a developer, it’s quite likely that you’re going to work on a React project in the future. Perhaps an existing project, or maybe your team will want you to work on a brand new app based on React.
2. A lot of tooling today is built using React at the core. Popular frameworks and tools like Next.js, Gatsby and many others use React under the hood.
3. As a frontend engineer, React is probably going to come up in a job interview.
Those are all good reasons, but one of the reasons I want you to learn React is that it’s great. It promotes several good development practices, including code reusability and components-driven development. It is fast, it is lightweight and the way it makes you think about the data flow in your application perfectly suits a lot of common scenarios.