How React really work?
Tags: javascript, react, webdev, tutorial
Published: 2021-05-05 05:53:23

When it comes to learning React, you will come across at least one article or a talk on YouTube by _Kent C Dodds_. I am a huge fan of this guy. His blogs and videos are simply awesome. I will highly recommend you to go and read his blogs and listen to his talks on YouTube. I got an inspiration to write this article by one of his talks on YouTube. 

You will find a lot of tutorials online about '_Writing your first program in React_' or describing various features offered by React but you will hardly find any posts or videos that will explain you what happens behind the scenes.

###Background

To understand how React works behind the scenes, it’s really important to have basic understanding of how to work with DOM elements using JavaScript. The program below shows how to display 'Hello World' text in the webpage.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ily9hb2a4nfhzdy5zoyp.png)

React offers component based approach which means you can divide your page into components. In order to write react components we need two JS files.
1. **React:** which offers functions for creating react elements (similar to createElement())
2. **ReactDOM:** which offers functions for rendering react elements to the DOM (similar to append())  

Once included, these JS files will give you access to two global objects namely **React** and **ReactDOM**. In order to create React elements, React object provides a function named __createElement__. This function takes following arguments.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m76cl762r8p0woo9yz8g.png) In above function definition, the parameter children can be an array which can have child elements created using the same __createElement__ command. Now, we have React elements but there has to be a way to render these elements to the DOM and _ReactDOM_ does that for you. It provides a method called **render** that helps you to render elements into the DOM.

Now, let's say we want to create a DOM structure like this.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipard59725mwsbp1sj47.png) React code for this will be as shown below.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gia71xlj2fjul917m3ql.png) In real life projects, it is not possible to write code using the syntax mentioned above. This is where JSX comes into picture.

###What is JSX?

**JSX** is a syntactic sugar on top of raw React APIs. It looks a lot like HTML but it is not. Your browser does not understand JSX so it needs something which will compile JSX into browser readable format. Most of the modern day apps use **babel** for this. In the image shown below, on left you can see the JSX syntax for displaying Hello World and on the right you will see how babel compiles the code using React library functions.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz90btak15hru633ijdu.png) If you’d like to see how JSX gets compiled to JavaScript, check out the online babel REPL [here](https://babeljs.io/).

To display 'Hello World' using JSX, you have to include babel script in your code. Now your code will look like this.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3doj6yy1634vtg0okrrq.png) So, basically we tell babel to compile our JSX code on the fly. You can see the compiled version in the browser as shown below.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf2zise76exclaqjm12k.png) If you have object of props, you can pass it to element tag as shown below.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69hssmbm9kl7emcvmlxa.png)

###Final Thoughts

So as far as the basics are concerned, this is all what you need. Frankly speaking this is not at all required to get you started. You can start React without this but it's always good to learn about the basics. It gives you a kind of confidence to learn more about the language otherwise you keep on having that feeling of missing the first step. And I strongly believe whenever you are learning something new, it should always start with your first step. Good luck :) 

Thank you for reading this article. Let me know your thoughts in comments section.
 
###References:

I would really recommend you to follow this reference material for more information.

1. Excellent talk by Kent C Dodds
https://www.youtube.com/watch?v=SAIdyBFHfVU 
2. Introducing JSX https://reactjs.org/docs/introducing-jsx.html#embedding-expressions-in-jsx