React Js:
Let me explain this with a practical example.
Imagine your favourite celebrity posting a photograph on Facebook. Now you go and like the image and then you suddenly see that lets check out the comments too. Now while you are browsing over comments you see that likes count increased by count 100 since you liked the picture. Yes and there was no reload of the page, just somehow magically the count changed. This magic my friend is react.js.
So to explain it better, imagine your web page. Again I will take example of Facebook since everybody uses it and common they created react.js :D . So billions of posts, so much traffic, millions like count. How do they manage all that a blazing speed. To do that they come up a library called react.js
Imagine a post on facebook. Now this post which is a container consists of multiple small parts like likes,comments,share,commentbox called components. So yes with react you page can be broken down into components. Now you must be wondering why do we do this, we already have awesome frameworks on front end? Well if you dont you are certainly going to be amused now.
HOW IT WORKS:-
Entire web is based on html. It all started with a simple HTML code and people went wohoo in the 90’s . Next came Javascript in 1996 with ways to interact with the HTML DOM(Document Object Model). After that was established came Jquery to change the contents of the HTML DOM. Next after that in 2009 came Angular.js as the daddy of front end frameworks which gave a full fledged option to create a strong frontend. Now if you see all these work on HTML code i.e you change HTML on basis of javascript(jquery, angular etc.). Now Javascript is much more powerful than HTML, Facebook considered this fact and decided to create the HTML itself from Javascript( dynamic content, if you know AJAX you should be able to relate to it) for which they created react.js. So to sum it up react.js creates your html from you javascript .
Now you know about components and how they work. You know react.js creates html from Javascript. But, reactjs itself is not written in vanilla javascript. React.js is written in something JSX(Java script XML). But HTML is created from Javascript right? So how do we get that JS from JSX. For this we use transpiler called Babel. Babel converts your JSX to JS which you can put in your html to create content dynamically.
WHAT MAKES IT FAST:
React has a concept of Virtual DOM. So say you html has this transpiled generated output javascript which creates your html. Now say whenever anything is changed in a particular component that component changes. Now these changes are not pushed to the DOM directly, but instead it is pushed to something called VirtualDOM. Now at this point there is a diff which occurs between VirtualDOM and real HTML DOM and changes are pushed to the realDOM. This is what makes it fast. Now trying linking to example I shared at the beginning of the answer. It should be clear now.
BENEFITS:
- Single Page Application.
- Entire page remains same but only few components change.
- Code is easy to scale and maintain.
- More functionalities as HTML is created from JS
- Isomorphic Rendering(Server side rendering)
- Blazing speed
Text from :Harkirat saluja from quora.
Comments
Post a Comment