Rendering using React
Entry point for every SPA application is initial HTML file with one empty div. That div, after loading SPA framework, is filled by SPA framework rules (in this case React). Body of initial HTML that we get as result of 'create-react-app' looks as follows:
public/index.html
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
As we can see, there is single <noscript> tag and single empty div having "root" id. <noscript> tag is HTML tag whose content is ignored by all browsers that support Javascript. In case when web browser does not support it, it actually renders content inside the <noscript> tag (effectively ignoring <noscript> tag).
Root div is the place in HTML where we expect React to render implemented user interface.
Following source files (with explanation of details relevant for this topic) are an entry point of our React application:
src/index.js
// Imports React modules
import React from 'react';
import ReactDOM from 'react-dom/client';
import reportWebVitals from './reportWebVitals';
// Imports index.css for our SPA.
import './index.css';
// Imports our App component
import App from './App';
// Creating Root React Component / Node.
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
// Strict Mode component provides us with React warnings in case of potential problems, such as
// possible memory leaks. It performs this only when is run in Development environment.
<React.StrictMode>
<App />
</React.StrictMode>
);
// Measure performance in Development environment.
reportWebVitals();
Looking at this file, we see instruction to fetch "root div" utilizing document.getElementById() method and that div is provided as input to ReactDOM.createRoot() method. From this point, React handles rendering "under the hood", meaning that in the rest of our source code we will not manipulate DOM directly (utilizing document object), but delegate actual DOM manipulations to React modules.
Root.render() method as an input takes two components: React.StrictMode and App. App is provided as the child of React.StrictMode component.
This HTML-like syntax is actually JSX. JSX is extension of Javascript syntax that looks similar to HTML (or HTML template language). It has full power of Javascript and practically every section written in JSX is actually preprocessed and executed as plain Javascript in the background. Instruction <App /> in HTML means nothing (it is invalid HTML tag), but in this case, it means "Render App Component" to this place.
In the src/App.js one can see App Component and JSX utilization to define its content.
src/App.js
// Importing svg React logo and App.css content
import logo from './logo.svg';
import './App.css';
// App component
function App() {
// JSX to render on the interface
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
In the implementation of App component, we can see interface that is actually rendered (React logo with link to "Learn React"). Term "className" utilized in JSX has the meaning of "class" in regular HTML. Since "class" is keyword in Javascript, that ambiguity had to be solved by defining another term for HTML class attribute.
Style of the page is defined in src/App.css.
src/App.css
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Rendering part of our application is covered through React components. Hierarchy of them is defined utilizing JSX, while actual rendering to the DOM is done by React.