Agile Coder Logo
AgileCoder
Beginner

Setting up a React Project From Scratch

Learn how to build a simple React app from scratch using just HTML and JavaScript. This guide walks you through adding React and ReactDOM via CDN, using JSX with Babel, and creating your first functional component—all in a single HTML page. Perfect for beginners!

Smruti Ranjan
February 3, 2025
3 min read
#ReactJS#Scratch#Without CRA
Setting up a React Project From Scratch
Advertisement

If you know HTML and JavaScript, you’ve likely created a simple webpage like this:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>A Boring HTML Page</title>
</head>
<body>
	<h2>Hello World</h2>
</body>
</html>
↕ click to expand

But what if we want to add React to this page? Since we can include additional libraries using the <script> tag, we can integrate React by adding two essential libraries:

  • react
  • react-dom

Adding React to a Simple HTML Page

Here’s how you can add React using a CDN:

HTML
&#x3C;!DOCTYPE html>
&#x3C;html lang="en">
&#x3C;head>
  &#x3C;meta charset="UTF-8">
  &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0">
  &#x3C;title>React Bare Setup&#x3C;/title>
  
  &#x3C;script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin>&#x3C;/script>
  &#x3C;script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin>&#x3C;/script>
&#x3C;/head>
&#x3C;body>
  &#x3C;div id="app">&#x3C;/div>

  &#x3C;script src="index.js">&#x3C;/script>
&#x3C;/body>
&#x3C;/html>
↕ click to expand

Now, let’s create an index.js file to write our React code:

JSX
const rootElement = document.getElementById('app');
const root = ReactDOM.createRoot(rootElement);

root.render(
  &#x3C;h2>Hello World&#x3C;/h2>
);
↕ click to expand

The Problem with JSX

The code above uses JSX (JavaScript XML), which is not natively supported by browsers. JSX needs to be transpiled into plain JavaScript before the browser can execute it.

To understand this better, here’s how JSX works under the hood:

JSX
const rootElement = document.getElementById('app');
const root = ReactDOM.createRoot(rootElement);

root.render(
  React.createElement("h2", null, "Hello World")
);
↕ click to expand

This works because React.createElement() is a function that generates the necessary JavaScript representation of the component.

Transpiling JSX with Babel

To use JSX directly, we need to add Babel to our HTML file:

HTML
&#x3C;!DOCTYPE html>
&#x3C;html lang="en">
&#x3C;head>
  &#x3C;meta charset="UTF-8">
  &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0">
  &#x3C;title>React Bare Setup&#x3C;/title>
  &#x3C;script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin>&#x3C;/script>
  &#x3C;script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin>&#x3C;/script>

  &#x3C;script src="https://unpkg.com/@babel/standalone/babel.min.js">&#x3C;/script>
&#x3C;/head>
&#x3C;body>
  &#x3C;div id="root">&#x3C;/div>

  &#x3C;script type="text/babel" src="index.js">&#x3C;/script>
&#x3C;/body>
&#x3C;/html>
↕ click to expand

Now, we can write JSX in index.js and it will work fine:

JSX
const rootElement = document.getElementById('root');
const root = ReactDOM.createRoot(rootElement);

root.render(
  &#x3C;h2>Hello World&#x3C;/h2>
);
↕ click to expand

Creating a React Component

Now, let’s create a simple React component:

JSX
const rootElement = document.getElementById('root');
const root = ReactDOM.createRoot(rootElement);

const HelloComponent = () => {
  return (
    &#x3C;h2>Hi There&#x3C;/h2>
  )
}

console.log(HelloComponent);

root.render(
  &#x3C;HelloComponent />
);
↕ click to expand

If you check the console, you’ll notice something interesting. The HelloComponent is just a JavaScript function! This proves that React components are nothing but JavaScript functions that return JSX.

Conclusion

With this setup, you’ve successfully created a React project from scratch without any build tools like Webpack or Vite. This is useful for quick prototyping or learning React without setting up a complex development environment.

For production, though, using a framework like Create React App or Vite is recommended for better performance and scalability.

Happy coding! 🚀

Advertisement

Behind-the-build, in your inbox.

New tutorials, book updates, and behind-the-scenes notes from the studio. No schedule, no spam.

Comments

Leave a comment

Comments are moderated before appearing.