1. Deep Dive: The React Foundation
React is just JavaScript. If you struggle with React, 90% of the time you are actually struggling with JavaScript concepts like closures, `this`, or reference equality.
1.1 Reference vs. Value
Primitives (string, number, boolean) are passed by value. Objects and Arrays are passed by reference.
Crucial for React: React compares props using "shallow comparison" (===). If you mutate an object (obj.name = 'new'), the reference is the same, so React won't know it changed and won't re-render.
1.2 Declarative vs. Imperative
Imperative (Old): "Loop through the array. Create a new variable. If index is even, push to array."
Declarative (React/Modern): "Map this array to that array."
We use .map(), .filter(), and .reduce() because they return new arrays (immutability) and describe what we want, not how to get it.
1.3 Industry Standard: Immutability
Senior devs never mutate data. They create copies.
Bad: user.name = "Alice"
Good: const updatedUser = { ...user, name: "Alice" }
This uses the Spread Operator to copy properties to a new object.
Build "The Data Transformer" Utility
As a data analyst, I need a utility to process raw order data so that I can view revenue from shipped orders only.
- Filter the list to keep only "shipped" orders.
- Map over them to create an array of strings: "Order #ID: $AMOUNT".
- Reduce the list to calculate the total revenue of shipped orders.
- Constraint: Use
.map(),.filter(), and.reduce(). Noforloops.
const orders = [
{ id: 1, status: "pending", amount: 50 },
{ id: 2, status: "shipped", amount: 100 },
{ id: 3, status: "cancelled", amount: 20 },
{ id: 4, status: "shipped", amount: 150 },
];
- Chaining: Perform all operations in a single chain without intermediate variables.
- Immutability: Ensure
ordersarray remains unchanged. - TypeScript: Define an
Orderinterface.
3. The Bug Hunt
🐛 Scenario: The Accidental Mutation
The Setup:
const user = { name: "John", address: { city: "NY" } };
const updatedUser = { ...user };
updatedUser.address.city = "LA";
console.log(user.address.city); // What does this print?
The Bug: You expected user.address.city to stay "NY", but it changed to "LA".
The Task:
- Explain Shallow Copy vs Deep Copy.
- Fix the code so that
useris not affected (use nested spread orstructuredClone). - Explain why
updatedUseris not a deep copy. - Fix it using
structuredClone()or JSON parse/stringify.
4. Node.js Fundamentals
Full-stack awareness is required. You don't need to be a backend expert, but you must understand the environment your React app interacts with.
- Runtime: Node.js allows JS to run outside the browser (Server-side).
- NPM/Yarn: Managing dependencies in
package.json. - Scripts: How
npm run buildtriggers a Node script to compile your React code.
5. Test on your own
Verify your JS Knowledge
- Console Check: Open your browser console. Type
[] == []. It returnsfalse. Why? (Reference equality). - Closure Check: Write a loop
for (var i=0; i<3; i++) { setTimeout(() => console.log(i), 100) }. It prints "3, 3, 3". Changevartolet. It prints "0, 1, 2". Explain why to yourself.