Skip to main content

Tutorial NextJS - Day 2

Hari 2 – Navigasi, Layout, & Dynamic Routing Setelah berhasil melakukan instalasi dan membuat halaman pertama di Hari 1 , pada Hari 2 ini kita akan membuat website menjadi lebih hidup. Kita akan belajar cara membuat menu navigasi agar bisa pindah-pindah halaman, membuat tampilan menu yang konsisten (Layout), dan membuat halaman artikel yang pintar (Dynamic Routing). Tutorial ini tetap menggunakan Windows + VS Code dan melanjutan project nextjs-basic dari hari sebelumnya. 1. Navigasi Antar Halaman ( <Link> ) Di HTML biasa, kita menggunakan tag <a> untuk berpindah halaman. Namun, di Next.js kita menggunakan komponen khusus bernama <Link> . Kenapa harus pakai <Link> ? Jika pakai <a> , layar akan berkedip (loading ulang) setiap pindah halaman. Dengan <Link> , perpindahan halaman terjadi instan tanpa refresh. 1.1 Edit Halaman Home Kita akan menambahkan tombol untuk pergi ke halaman About. Buka file src/app/page.tsx , lalu ganti isinya dengan kod...

Common React JS Mistakes Beginners Make (And How to Fix Them)


Introduction

React JS is powerful, but many beginners struggle not because React is hard — but because of common mistakes made early on. These mistakes can lead to confusing bugs, poor performance, and messy code that becomes difficult to maintain.

In this article, we’ll cover the most common React JS mistakes beginners make, explain why they happen, and show how to fix them properly.

If you’re learning React or recently started building projects, this guide will save you hours of frustration.


1. Modifying State Directly

One of the most frequent beginner mistakes is changing state directly instead of using the state updater function.

❌ Wrong Example

count = count + 1;

This does not trigger a re-render.

✅ Correct Way

setCount(count + 1);

Why This Matters

React relies on state updates to know when to re-render. Direct mutation breaks that mechanism and causes unpredictable UI behavior.


2. Using State When It’s Not Needed

Beginners often store everything in state, even when it never changes.

❌ Overusing State

const [title, setTitle] = useState("Welcome");

If the value never changes, state is unnecessary.

✅ Better Approach

const title = "Welcome";

Why This Matters

Unnecessary state:

  • Makes components more complex

  • Triggers extra re-renders

  • Reduces code clarity


3. Creating Very Large Components

Another common mistake is writing huge components that handle too many responsibilities.

Symptoms

  • One file with hundreds of lines

  • Hard to read and debug

  • Difficult to reuse

✅ Best Practice

Break UI into smaller components:

  • One component = one responsibility

  • Easier to test

  • Easier to reuse


4. Forgetting the key Prop in Lists

When rendering lists, React needs a unique key to track elements efficiently.

❌ Missing Key

items.map(item => <li>{item.name}</li>);

✅ Correct Usage

items.map(item => <li key={item.id}>{item.name}</li>);

Why This Matters

Without keys, React may:

  • Re-render incorrectly

  • Cause UI glitches

  • Reduce performance


5. Using Array Index as Key

Using the array index as a key seems convenient, but it can cause bugs.

❌ Bad Practice

items.map((item, index) => ( <li key={index}>{item.name}</li> ));

✅ Better Practice

Use a stable unique identifier:

<li key={item.id}>{item.name}</li>

Indexes change when items are reordered — IDs don’t.


6. Confusing Props and State

Many beginners struggle to decide what should be state and what should be props.

Simple Rule

  • State: data that changes inside the component

  • Props: data passed from parent to child

❌ Common Mistake

Trying to modify props directly.

✅ Correct Pattern

  • Keep state in the parent

  • Pass data and callbacks via props


7. Running Logic Directly Inside JSX

Putting complex logic inside JSX makes code harder to read.

❌ Messy JSX

<h1>{user && user.isAdmin ? "Admin" : "User"}</h1>

✅ Cleaner Approach

const role = user?.isAdmin ? "Admin" : "User"; <h1>{role}</h1>

Clean JSX improves readability and maintenance.


8. Ignoring the Dependency Array in useEffect

useEffect is powerful, but misuse can cause bugs or infinite loops.

❌ Missing Dependencies

useEffect(() => { fetchData(); });

✅ Correct Usage

useEffect(() => { fetchData(); }, []);

Why This Matters

The dependency array controls when effects run. Ignoring it can cause:

  • Repeated API calls

  • Performance issues

  • Unexpected behavior


9. Fetching Data in the Wrong Place

Some beginners fetch data directly inside the component body.

❌ Wrong

fetchData();

✅ Correct

useEffect(() => { fetchData(); }, []);

Side effects belong in useEffect, not in the render flow.


10. Expecting React to Handle Everything

React handles the UI layer — not everything else.

React does NOT:

  • Manage backend logic

  • Handle databases

  • Replace proper architecture

Understanding React’s role helps you build better systems overall.


How to Avoid These Mistakes

Here’s a simple checklist:

  • Use state only when necessary

  • Break components into small pieces

  • Keep JSX clean

  • Understand data flow (top-down)

  • Read error messages carefully

Mistakes are part of learning — React becomes easier once patterns click.


Conclusion

Most React JS beginner mistakes come from misunderstanding fundamentals, not lack of intelligence. By learning these common pitfalls early, you can write cleaner, faster, and more maintainable React code.

Fixing these mistakes will immediately improve your confidence and productivity as a React developer.

Comments

Popular posts from this blog

Why Your React App Shows a Blank Page (And How to Fix It)

Introduction One of the most frustrating problems when working with React JS is opening your app and seeing… nothing . No error message. No UI. Just a blank page . This issue happens frequently, especially after deployment or routing changes. In this article, we’ll explain why React apps show a blank page , the most common causes , and how to fix them step by step . What a “Blank Page” Usually Means in React A blank page usually means: React failed to render the UI JavaScript error stopped execution Routing or build configuration is broken Most of the time, the issue is not React itself , but how the app is configured. 1. JavaScript Error in the Console The first thing you should always check is the browser console. How to Check Open the app Right click → Inspect Open the Console tab If you see a red error message, React stopped rendering. Common Examples Cannot read property of undefined Unexpected token Module not found Fix Read the...

Nginx vs Apache: Which Web Server Should You Use on a VPS?

Introduction When setting up a VPS for hosting websites or applications, one of the first decisions you’ll face is choosing a web server. The two most popular options are Nginx and Apache . Both are powerful, widely used, and production-ready — but they work very differently. In this article, we’ll compare Nginx vs Apache , explain how each one works , highlight their strengths and weaknesses , and help you decide which web server is better for your VPS setup . What Is a Web Server? A web server is software that: Receives requests from browsers Processes those requests Serves HTML, CSS, JavaScript, or API responses Popular web servers include: Nginx Apache LiteSpeed Caddy Among them, Nginx and Apache dominate most VPS environments . What Is Apache? Apache has been around since the mid-1990s and was the most widely used web server for many years. How Apache Works Apache uses a process-based or thread-based model : Each request is handled by a p...

React Build vs Development Mode: What’s the Difference?

Introduction Many React developers are surprised when their app works perfectly during development — but behaves differently after deployment. Features break, routing fails, environment variables disappear, or performance changes dramatically. Most of these issues come from not understanding the difference between React’s development mode and production (build) mode . In this article, we’ll explain: What development mode really does What changes in production builds Why bugs appear only after deployment How to avoid common build-related mistakes What Is Development Mode in React? Development mode is what you use when running commands like: npm start npm run dev In this mode, React prioritizes developer experience , not performance. Key Characteristics of Development Mode Detailed error messages Helpful warnings Hot reloading (instant updates) Slower performance (on purpose) Extra checks for unsafe patterns Development mode helps you find bugs e...