This document provides a detailed walkthrough of how I set up the project from scratch. I will explain each step I took, along with the reasoning behind it.
Heads-up: This will be a very text-heavy document. I might skip minor details to keep it concise and easy to digest while focusing on the essential parts.
The first step was to create a new folder for the project. (This was a crucial step because if the project folder does not exist, the project does not exist).
mkdir react-dashboard-app
Of course, I could have used the GUI, but I chose the CLI.
If you're using a GUI, open the folder manually. Otherwise, use the command line:
cd react-dashboard-app
To create a package.json
file, I initialized a Node.js project:
npm init -y
-y
selects all default configurations. This file holds metadata about the application, including the author, version, scripts, dependencies, dev dependencies, etc.
Since this documentation reflects my progress day by day, you'll see installations happening in steps. More dependencies will be added as needed later.
npm install react react-dom
Next, I installed webpack
, webpack-cli
, and webpack-dev-server
as dev dependencies:
npm install --save-dev webpack webpack-cli webpack-dev-server
webpack
: The core Webpack librarywebpack-cli
: CLI support for Webpackwebpack-dev-server
: Enables local development features like hot reloading
I installed these as dev dependencies because they are only needed during development and should not be included in the production bundle.
Since browsers do not understand JSX, I installed Babel to transpile the JSX code:
npm install --save-dev babel-loader @babel/core @babel/preset-react @babel/preset-env
babel-loader
: Helps Webpack process.js
and.jsx
files@babel/core
: The core Babel library@babel/preset-react
: Enables JSX support@babel/preset-env
: Ensures compatibility with modern JavaScript features
This setup prevents us from writing React.createElement('div', null, 'Hello, world!');
manually, making React development easier.
Next, I created a webpack.config.js
file in the project's root directory. This file tells Webpack how to bundle the project.
module.exports = {};
This tells Webpack where to start bundling:
module.exports = {
entry: "./src/index.js",
};
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
};
path.resolve(__dirname, "dist")
: Ensures the output is placed in a proper directoryfilename: "bundle.js"
: Defines the bundled file name
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
devServer: {
port: 3000,
static: "./dist",
hot: true,
},
};
To handle JavaScript and CSS files, I added module rules:
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
devServer: {
port: 3000,
static: "./dist",
hot: true,
},
module: {
rules: [
{
test: /\.(js|jsx)$/, // Process .js and .jsx files
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.css$/, // Process CSS files
use: ["style-loader", "css-loader"],
},
],
},
};
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
devServer: {
port: 3000,
static: "./dist",
hot: true,
},
module: {
rules: [
{
test: /\.(js|jsx)$/, // Process .js and .jsx files
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.css$/, // Process CSS files
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "public", "index.html"),
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
};
HtmlWebpackPlugin
: Injects the bundled output intoindex.html
resolve.extensions
: Allows imports without specifying file extensions
I created a .babelrc
file in the root directory and added:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
This enables Babel to transpile both modern JavaScript and React JSX syntax.
I created the following structure:
react-dashboard-app/
├── public/
│ ├── index.html
├── src/
│ ├── index.js
│ ├── App.js
│ ├── styles.css
├── .babelrc
├── webpack.config.js
├── package.json
/*
This is the entry-point of our application.
It renders the main App component into the root element.
We start off by importing 'createRoot' from ReactDOM's Client file.
Then, we import the App component.
Finally, we render the App component into the root element by targetting the 'root' div present in the index.html file.
*/
import { createRoot } from "react-dom/client";
import App from "./App";
const root = createRoot(document.getElementById("root"));
root.render(<App />);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Dashboard Application</title>
</head>
<body>
<!-- This is the div that holds everything -->
<div id="root"></div>
</body>
</html>
At this stage, the project is set up with:
- A properly structured folder hierarchy
- Webpack and Babel configurations
- Essential dependencies installed
- React and JSX transpilation working
This setup serves as a strong foundation for building the React dashboard application.
Next up: Understanding the Problem Statement & coming up with a Plan.