š§ Learning React from the Ground Up: Understanding Raw React APIs
React is the most widely used frontend framework in the worldāand itās not just for the web. It powers mobile, desktop, VR, and even terminal interfaces. But before we get into the fancy tooling or JSX syntax, letās take a step back and understand how React works under the hoodāstarting with the raw React APIs.
In this post, weāll break down the basics of Reactās core building blocks and how they map to what the browser does. This foundational knowledge will make you a stronger React developer, and help demystify whatās really happening behind the scenes.
React abstracts the imperative browser APIs (like document.createElement
) to give developers a declarative way to build user interfaces. This distinction between imperative and declarative programming is key to understanding why React is so powerful.
š§ Imperative vs Declarative Programming Want to understand the difference? Check out Imperative vs Declarative Programming
Even though React gives us a high-level API, underneath it still relies on the same low-level DOM manipulation weāre used to. In fact, you can see where that happens in the React source code here.
To build a web app with React, we need two JavaScript files:
document.createElement
)appendChild
)In a real-world app, youād usually import these from npm:
import { createElement } from 'react'import { createRoot } from 'react-dom/client'
But for simplicity, in our learning environment, weāll use pre-bundled files served from the /public
folder:
/react.js
/react-dom/client.js
These files are delivered via esm.shāa service that helps us load React modules directly in the browser.
React has a core function for building UI components: createElement
. This is what JSX compiles to under the hood.
Letās build a simple example:
import { createElement } from '/react.js'import { createRoot } from '/react-dom/client.js'const elementType = 'h1'const elementProps = { id: 'element-id', children: 'Hello world!' }const reactElement = createElement(elementType, elementProps)const root = createRoot(rootElement)root.render(reactElement)
š” Tip: Try running
console.log(reactElement)
to see how itās structured!
props
and children
In React, props are short for āproperties.ā Theyāre the way we customize elementsājust like setting attributes in HTML.
One special prop is children
, which represents the content nested inside the component.
There are multiple ways to define children:
children
propcreateElement('h1', { children: 'Hello world!' })
createElement('h1', null, 'Hello', ' ', 'world!')
createElement('h1', null, ['Hello', ' ', 'world!'])
All these methods are equivalent from Reactās perspective!
Letās create a DOM structure like this:
<div class="container"><span>Hello</span><span>World</span></div>
To do this in raw React, we write:
const hello = createElement('span', null, 'Hello')const space = ' 'const world = createElement('span', null, 'World')const container = createElement('div',{ className: 'container' },hello,space,world)createRoot(rootElement).render(container)
ā ļø If you try to pass
children
as a prop object with multiple elements, React might warn you to use akey
āmore on that later!
Hereās a more complex example:
<div class="container"><p>Here's Sam's favorite food:</p><ul class="sams-food"><li>Green eggs</li><li>Ham</li></ul></div>
Using Reactās APIs:
const paragraph = createElement('p', null, "Here's Sam's favorite food:")const list = createElement('ul',{ className: 'sams-food' },createElement('li', null, 'Green eggs'),createElement('li', null, 'Ham'))const container = createElement('div',{ className: 'container' },paragraph,list)createRoot(rootElement).render(container)
Understanding how React works at its coreābefore introducing JSX or build toolsāis a fantastic way to develop a strong foundation. You now know:
createElement
to build componentschildren
work in different formsAnd remember: this is exactly how React works behind the scenes, even when youāre writing JSX!
⨠Next time, weāll look at how JSX simplifies all of this syntax while doing exactly the same thing under the hood.
Happy coding! š If you want to dig deeper, check out the official React docs. And stay tunedāweāll soon build more complex UI with actual interactivity!
Quick Links
Legal Stuff
Social Media