React Fundamentals: Intro to React Raw APIs

Full-Stack Developer, Entrepreneur and Co-Founder of Coderplex.
Building https://coderplex.in
Checkout my portfolio at https://bhanuteja.dev
Search for a command to run...

Full-Stack Developer, Entrepreneur and Co-Founder of Coderplex.
Building https://coderplex.in
Checkout my portfolio at https://bhanuteja.dev
No comments yet. Be the first to comment.
In this series, I will write different blog posts based on workshop content from [Kent C. Dodds](https://kentcdodds.com)'s [EpicReact.Dev](https://epicreact.dev) as I go through it.
Hello World ๐ Welcome to the 4th article of the series My Review of Kent C. Dodds's EpicReact.Dev. Please note that this blog post series is just my review of the EpicReact.Dev workshop material. I am just trying to explain what I learned and unders...
Authored in connection with the Write With Fauna program. Table of Contents Authentication Setting up Fauna in Next.js Installing Fauna Setting up Migrations Tool Authentication and Authorization in Fauna Next.js Serverless Function Setup for Fa...

I wanted to start blogging in Aug of 2020. I decided to use @hashnode for my blog. It was the best decision that I made. I wrote 27 technical articles so far. Most of them are about frontend web development. A thread ๐งต about each of these articles i...

Next.js has become my go-to framework for almost every project that I make. So, I made a starter template that I can just use and get started easily. In this article, I will show you how to use the starter template that I made and deploy it with Verc...

In this article, I will list out all the git commands that I use very frequently. This is not in any way a complete list, just the commands that I use very often. This is intended to be used as a quick reference to perform an action that you want. ...

In this article, we will see the order in which different useEffect callbacks and cleanups happen. We will also see how it differs when the app mounts, unmounts, updates. This image is taken from https://github.com/donavon/hook-flow. I took the ex...

Bhanu Teja Pachipulusu's blog
29 posts
Developer, Indie Maker and Blogger. Currently building MDX.one
Checkout my portfolio
Welcome to the third article of the My Review of Kent C. Dodds's EpicReact.Dev Series which is based on the workshop material from EpicReact.Dev by Kent C. Dodds. If you haven't read the previous articles of the series yet, please go and read them before continuing through this article.
Here are the previous articles.
In this article, You are going to learn the very basics of React. We will just be working with basic HTML and javascript using React raw APIs. We will not even be using any JSX in this article(If you don't know what JSX is, don't worry, we will learn about it in the next article.) You will see why it is difficult to work with React raw APIs. Many people skip over these fundamentals before learning React, but it is important to know about these abstractions to understand some things in React which we will see in the next article.
We will follow the similar format of the workshop - meaning for every topic, we will first introduce what we are trying to achieve, then we will see how to achieve that.
Our goal is to render Hello World using basic javascript.
Our HTML currently has the following
<div id="root"></div>
We want our HTML to be:
<div id="root">
<div class="container">Hello World</div>
</div>
We can achieve the above result by making use of Javascript's document API.
// Fetches the element with id as `root` from DOM
const rootElement = document.getElementById("root")
// Creates an element with `div` tag
const helloWorldElement = document.createElement("div")
helloWorldElement.textContent = "Hello World"
helloWorldElement.className = "container"
// Appends the helloWorldElement to the rootElement
rootElement.append(helloWorldElement)
Let's break down what we are doing here.
id as root from DOM.Even though the above code is very clear, I broke it down into steps, because in the following section, we will use these exact steps to achieve this, but by using React APIs.
React uses the same document API that we have seen before under the hood. But it abstracts it away and gives you easy to use and intuitive API to work with
Let's try to create the same hello world markup that we did before, this time using React.
// Fetches the element with id as `root` from DOM
const rootElement = document.getElementById("root")
// Creates an element with `div` tag
const helloWorldElement = React.createElement("div", {
className: "container",
children: "Hello World"
})
// Appends the helloWorldElement to the rootElement
ReactDOM.render(helloWorldElement, rootElement)
Before we understand this code, observe that we have used React and ReactDOM, which are not a part of basic javascript. Hence, they should be added before they become available to us.
Let's add them using their CDN scripts. We will be using unpkg's CDN.
<script src="https://unpkg.com/react@16.7.0/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0/umd/react-dom.production.min.js"></script>
After adding this, we will be able to access React and ReactDOM from within javascript.
Let's try to understand the code now.
id as root from DOM.document API to get the root element.React.createElement().children. children is basically a replacement of what we want inside of the HTML tag that we create. <div>Hello World</div>, we will create a React element with a div tag and set the children's property to Hello World.ReactDOM.render()element1 to element2. You would do ReactDOM.render(element1, element2).Can you see and appreciate how similar both the React APIs and document APIs are. With the knowledge that we have let's try to create the below markup using React.
Lets try to create the following markup with React.
<div id="root">
<div>
<span>Hello</span>
<span>World</span>
</div>
</div>
Before doing this, you need to know that the children property that we have seen before will also accept an array as its value.
For example, both of the following calls will produce the same HTML output.
// 1.
React.createElement("div", { children: "Hello World" })
// 2.
React.createElement("div", { children: ["Hello", " ", "World"] })
Now that we know this, let's try to create the given markup.
// Fetches the element with id as `root`
const rootElement = document.getElementById("root")
// Creates a `span` element with content as `Hello`
const helloElement = React.createElement("span", {children: "Hello"})
// Creates a `span` element with content as `World`
const worldElement = React.createElement("span", {children: "World"})
// Let's put the above two elements in to a single div
const helloWorldElement = React.createElement("div", {
children: [helloElement, worldElement]
})
The above code will create the HTML markup that we want.
Note:
React.createElement can also take more than 2 arguments. The following two calls generate the same thing.
// 1.
React.createElement("div", {children: [element1, element2, element3]})
// 2.
React.createElement("div", {}, element1, element2, element3)
So, you can essentially unpack the children array and then add them as other arguments.
That's it for now.
In this article, you saw how verbose React raw APIs are. So, it becomes a little difficult to write code like this when there are a lot of elements and each element has different properties.
That is why React gave us a way to simplify this and write the code in a format called JSX which looks a bit similar to HTML. In the next article, we will see all about JSX and we will also see how to create custom components and style them with CSS.
If this was helpful to you, Please Like and Share so that it reaches others as well. To get email notifications on my latest articles, please subscribe to my blog by hitting the Subscribe button at the top of the page. You can also follow me on twitter @pbteja1998.
Links and References:
The next article in this series is already live. Click on the link below๐