⚛️ React Journey Day 1: From Zero to Hello World (With Java Dev Mindset)
Hey folks! 👋 Welcome to Day 1 of my Full Stack Journey — and today, we officially begin learning React, from scratch, as a backend developer!
🚀 Why Do We Need Node.js and npm to Run React?
If you’re from a Java background like me, think of this:
Concept | Java | React |
---|---|---|
Language | Java | JavaScript (JSX) |
Runtime | JRE / JDK | Node.js |
Build Tool | Maven / Gradle | npm (Node Package Manager) |
Node.js allows JavaScript to run outside the browser. React apps use Node to:
- Compile JSX into plain JS
- Run local development servers
- Install libraries (just like Maven dependencies)
npm is like Maven — it downloads, installs, and manages your frontend packages and scripts.
🔧 What is Vite? And Why Do We Use It?
Vite is a modern build tool that gives us:
- Instant startup
- Faster hot reload
- Simple setup
It's like the Spring Boot Initializr of the frontend world! 🧠
Feature | Spring Boot | Vite |
---|---|---|
Project Setup | start.spring.io | npm create vite |
Dev Server | Embedded Tomcat | Vite Dev Server |
🔄 Alternatives to Vite
- create-react-app (CRA) – older, heavier setup
- Parcel – zero config but less popular
- Webpack (manual) – expert level
- Next.js – production-ready framework on top of React
🔥 Today, Vite or Next.js is preferred in companies
📦 Setting Up React Project — Step-by-Step
✅ Prerequisite: Install Node.js (Mac & Windows)
Go to https://nodejs.org and download the LTS version:
- 🍏 Mac users: Download the macOS installer (.pkg)
- 🪟 Windows users: Download the Windows installer (.msi)
After installation, open Terminal or Command Prompt and run:
node -v
npm -v
🍏 Optional for Mac Users: If you use or want to use Homebrew (Mac’s terminal-based package manager), install Node with:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node
Otherwise, download the Node.js macOS installer (.pkg) from nodejs.org.
🧠 React Setup Commands — Explained Step-by-Step (for Java Devs)
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
✅ 1. npm create vite@latest my-react-app -- --template react
This command scaffolds a new React project using the latest version of Vite.
Part | Meaning |
---|---|
npm |
Node’s package manager — like mvn or gradle |
create vite@latest |
Use the latest official Vite project generator |
my-react-app |
Your app/project folder name |
-- |
Separates npm arguments from Vite ones |
--template react |
Use React as the frontend template |
Java Analogy: Like using Spring Initializr with a web dependency and generating a project zip.
✅ 2. cd my-react-app
Change directory into the newly created React app folder.
Java Analogy: cd my-spring-app
after generating a project using start.spring.io.
✅ 3. npm install
Installs all dependencies defined in package.json
— React, Vite, JSX tooling, etc.
Java Analogy: Like mvn install
to download all dependencies from pom.xml
.
📦 What Does npm install
Do?
This command installs all the dependencies listed in your project’s package.json
.
- 📥 Downloads required libraries (React, Babel, Vite, etc.)
- 🧱 Creates a
node_modules/
folder — like your local dependency JARs - 🧾 Generates a
package-lock.json
to lock versions
Think of it like running:
mvn install
in a Spring Boot project. It resolves and pulls all required dependencies.
🧠 Java Comparison
React (npm) | Java (Maven) |
---|---|
npm install |
mvn install |
Reads package.json |
Reads pom.xml |
Creates node_modules/ |
Uses .m2/repository |
Creates package-lock.json |
No exact lock, but fixed versions in <version> |
🔒 What is package-lock.json
and Why Is It Important?
When you run npm install
, it not only installs your dependencies but also creates a special file called package-lock.json
.
🧠 What It Does
- Locks the exact versions of all dependencies (and their sub-dependencies)
- Ensures everyone on your team uses the same versions
- Makes builds predictable and stable
"axios": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz"
}
🚫 What Happens Without It?
- You install
axios@1.4.0
- Your teammate installs
axios@1.4.5
- Unexpected bugs may crash production 💥
🧠 Java Analogy
React (npm) | Java (Maven) |
---|---|
package.json with ^1.4.0 |
<version>[1.4.0,2.0.0)</version> |
package-lock.json |
Pinning versions or lock plugin (in Gradle) |
✅ Summary
package-lock.json
= your project's version snapshot- Protects you from bugs in new versions
- Ensures consistent builds across all machines
✅ When Should You Run It?
- 📁 After downloading or cloning a React project
- 🔄 After deleting
node_modules/
- ➕ After adding new dependencies like
npm install axios
⚠️ Important Notes
- 🚫 Never push
node_modules/
to GitHub - ✅ Only commit
package.json
andpackage-lock.json
Once installed, you’re ready to run:
✅ 4. npm run dev
Starts the Vite development server.
- Hot reload enabled 🔁
- Watches for code changes 👀
- Serves your app on
http://localhost:5173
🌐
Java Analogy: Like ./mvnw spring-boot:run
to launch your backend locally.
⚙️ What Does npm run dev
Actually Do?
This command tells npm (Node Package Manager) to run the script named dev
from your project’s package.json
file.
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
So when you run npm run dev
, it executes Vite — your development server.
🔍 What Happens Internally?
- 🟢 Starts Dev Server → Runs at
http://localhost:5173
- 🔁 Hot Reload → Instantly reloads browser on code changes
- ⚡ Compiles JSX → Converts JSX to regular JavaScript using ESBuild
- 📁 Serves Static Files → Loads from
public/
andsrc/
✅ It's optimized for speed and instant feedback — perfect for development!
🧠 Java Comparison
React (Vite) | Java (Spring Boot) |
---|---|
npm run dev |
mvn spring-boot:run |
Starts Vite dev server on port 5173 |
Starts embedded Tomcat on port 8080 |
Hot-reloads UI as you type | Rebuild + restart on code changes |
💡 Why Use npm run dev
Instead of Just vite
?
- ✅ Ensures the right version of Vite is used from your local project
- ✅ Reads any custom configs from
vite.config.js
- ✅ More portable — works for all devs on your team
🎯 Summary
npm run dev
starts the Vite server using your project’s local config- It’s fast, hot-reload enabled, and watches your JSX/TSX code
- It's the frontend version of
spring-boot:run
for Java devs
🌐 You can now open http://localhost:5173
in your browser and start building your UI!
📦 What’s Inside package.json
?
You’ll see these scripts under the scripts
section:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
So when you run npm run dev
, it’s just calling vite
under the hood.
🧠 Summary Table
Command | What It Does | Java Equivalent |
---|---|---|
npm create vite@latest my-app -- --template react |
Create a new React app using Vite | start.spring.io project creation |
cd my-app |
Enter the project folder | cd my-springboot-app |
npm install |
Install frontend dependencies | mvn install |
npm run dev |
Run Vite dev server with hot reload | mvn spring-boot:run |
🎉 After this, you can open http://localhost:5173
in your browser and see your first React component in action!
📁 Folder Structure — Compared with Java
Java Project | React Project |
---|---|
src/main/java |
src/ (JSX code) |
src/main/resources |
public/ (static files) |
pom.xml |
package.json |
🧠 Your First Component (App.jsx)
Open App.jsx
and write:
function App() {
return (
<div>
<h1>Hello Anand!</h1>
<p>This is your first React component.</p>
</div>
);
}
export default App;
Java Comparison:
function App()
= like apublic class
return (<div>)
= like HTML template builderexport default App
= like your main class being exposed
🧩 What is JSX?
JSX = JavaScript + XML
It allows us to write:
<h1>Hello</h1>
inside JavaScript functions. It’s not natively supported by browsers, so tools like Babel (inside Vite) convert it to valid JS.
Supported File Extensions:
.js
— Plain JavaScript.jsx
— JavaScript with JSX.ts
— TypeScript.tsx
— TypeScript with JSX
🎯 Mini Practice
function App() {
const name = "Anand";
const year = new Date().getFullYear();
return (
<div>
<h2>🚀 Hello {name}!</h2>
<p>React Journey — {year}</p>
<button onClick={() => alert("Keep Learning!")}>Motivate Me</button>
</div>
);
}
Try it, run it, and smile when the alert works 😄
FILE : /my-react-app/src/App.jsx
✅ Wrapping Up
- You now know what
Node.js
&npm
are — like JDK + Maven - Vite is your Spring Boot of the frontend
- JSX is HTML + JavaScript (JSX = JavaScript + XML)
- You created and ran your first React component!
✅ From Day 2 onward: I’ll start using.tsx
files — because most companies today expect React with TypeScript.
It's strongly typed, safer, and aligns well with enterprise-grade standards. 💼⚛️🟦
Stick around — React is just getting fun! ⚛️
If you liked this post or are following the same path, drop a comment below — let’s learn React + TypeScript together! 💬🔥
Comments
Post a Comment