By Yeo Yong Kiat in ReactJS
Let's begin breaking down a React app by understanding the index.html and index.js files. Remember my post about how modern JS essentially has you just downloading a bare bones html file and a JS script that renders your app in your browser? Today we're going to see how it works by tearing apart these two index files.
As I've mentioned, it's the minimal html file that your browser downloads when accessing a Single-Page Application (SPA). It has almost nothing, except for the most fundamental components of a basic HTML file. Let's take a look by opening the index.html file in a code editor, which should look something like this:
<!--I've simplified the html by removing metadata--> <!DOCTYPE html> <html lang="en"> <head> <!--misc metadata goes here--> <title>React App</title> </head> <body> <div id="root"></div> </body> </html>
Notice how minimal this html file is, with almost no elements at all. How did it render into the webpage you saw when you ran "npm start" in the terminal? We'll get to that in a minute. For now, just note that the index.html file is just like any other html file. Also, take a look at the <body> section, where there is a <div id="root"> element. This is the element within which your entire app will be rendered, hence its id name. If you remove this element, your entire app vanishes.
Try removing the <div> element. Or renaming the "id". See what happens.
It's a bit too early to talk about how React works, so let's move onto the index.js file.
Let's load index.js into a code editor, which should look something like:
// Loading react libraries import React from 'react'; import ReactDOM from 'react-dom/client'; // Loading styles import './index.css'; // Loading app.js import App from './App'; // Creating root of app within <div id="root"> const root = ReactDOM.createRoot(document.getElementById('root')); // Rendering whatever App.js has into root root.render( <React.StrictMode> <App /> </React.StrictMode> );
Not easy for someone uninitiated to React's syntax, so let's break it down:
But some things look strange, doesn't it? Let's pause for while and go through them one by one:
Actually, no! You code your app within App.js instead, which means this entire index.js file can be left entirely untouched. To write our app, we focus on the App.js component and other components, which will be referenced within App.js. The purpose of index.js is just to render whatever App.js coded for into the <div id="root"> element of your index.html file.
In short, you don't need to touch anything in the index files for now. We'll come back to a more thorough explanation in future posts when you've grasped a little bit more about React. That's how learning React is like - you can't quite get everything first, you move onto other parts, and then you come back when you've learnt enough. Get used to it, React's a wild ride. And a fun one.
In the next post, we'll look into what the App.js file is.
Find more ReactJS stories on my blog. Have a suggestion? Contact me at [email protected].