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...

More Common React JS Mistakes Developers Still Make (And How to Avoid Them)

 

Introduction

Even after learning the basics of React JS, many developers continue to make mistakes that hurt performance, readability, and scalability. These are not beginner-only errors — they often appear in real projects, especially when apps start to grow.

In this article, we’ll look at common React JS mistakes developers still make, explain why they happen, and show better patterns to avoid long-term problems.


1. Re-Creating Functions on Every Render

Many developers define functions directly inside components without realizing the impact.

❌ Common Pattern

function App() { const handleClick = () => { console.log("clicked"); }; return <Button onClick={handleClick} />; }

While this works, the function is re-created on every render.

✅ Better Approach (When Needed)

const handleClick = useCallback(() => { console.log("clicked"); }, []);

Why This Matters

  • Causes unnecessary re-renders

  • Affects performance in large apps

  • Becomes problematic when passed to memoized components

⚠️ Note: Don’t overuse useCallback. Use it only when optimization is needed.


2. Overusing useEffect for Everything

useEffect is powerful, but many developers use it as a default solution for all logic.

❌ Misuse Example

useEffect(() => { setTotal(price * quantity); }, [price, quantity]);

✅ Better Approach

const total = price * quantity;

Why This Matters

  • Effects should handle side effects, not calculations

  • Overusing useEffect makes code harder to reason about

  • Leads to unnecessary renders


3. Ignoring Cleanup in Effects

Forgetting to clean up effects is a common source of memory leaks.

❌ Problematic Code

useEffect(() => { window.addEventListener("resize", handleResize); }, []);

✅ Correct Usage

useEffect(() => { window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); }; }, []);

Why This Matters

  • Prevents memory leaks

  • Avoids duplicated event listeners

  • Keeps apps stable over time


4. Storing Derived Data in State

Developers often store values in state that can be calculated from existing data.

❌ Unnecessary State

const [fullName, setFullName] = useState(""); useEffect(() => { setFullName(firstName + " " + lastName); }, [firstName, lastName]);

✅ Better Pattern

const fullName = `${firstName} ${lastName}`;

Why This Matters

  • Fewer bugs

  • Simpler logic

  • Less state to manage


5. Fetching Data Without Error Handling

Many apps fetch data but forget to handle failures.

❌ Incomplete Fetch Logic

fetch("/api/data") .then(res => res.json()) .then(setData);

✅ Safer Pattern

try { const res = await fetch("/api/data"); if (!res.ok) throw new Error("Request failed"); const data = await res.json(); setData(data); } catch (error) { setError(error.message); }

Why This Matters

  • APIs fail in real life

  • Users need feedback

  • Silent errors break UX


6. Putting Too Much Logic in Components

As apps grow, components often become overloaded.

Signs of This Mistake

  • Components handling UI + data + validation + API calls

  • Hard to test

  • Hard to reuse

✅ Better Architecture

  • Move logic to:

    • Custom hooks

    • Utility functions

    • Service layers

This keeps components focused on presentation, not everything else.


7. Forgetting to Handle Loading States

A common UX mistake is ignoring loading states.

❌ Missing Loading State

return <UserProfile user={user} />;

✅ Better UX

if (loading) return <Spinner />; if (error) return <ErrorMessage />; return <UserProfile user={user} />;

Why This Matters

  • Prevents broken UI

  • Improves perceived performance

  • Makes apps feel professional


8. Assuming React Automatically Optimizes Everything

React helps with performance, but it’s not magic.

React does NOT automatically:

  • Prevent unnecessary renders

  • Optimize heavy computations

  • Fix poor component design

Understanding React’s behavior is key to writing efficient code.


9. Not Using Strict Mode in Development

Many developers skip React Strict Mode.

Why You Should Use It

  • Detects unsafe lifecycle usage

  • Highlights potential bugs early

  • Encourages best practices

Strict Mode doesn’t affect production, but it helps you write better code.


10. Learning React Without Building Projects

One of the biggest mistakes is learning React only from tutorials.

Why This Is a Problem

  • Tutorials hide complexity

  • Real bugs appear only in real projects

  • Problem-solving skills don’t develop

✅ Best Way to Learn React

  • Build small projects

  • Break things intentionally

  • Debug your own mistakes


How to Avoid These Mistakes Long-Term

Here’s a practical mindset:

  • Keep components small

  • Use hooks intentionally

  • Avoid unnecessary state

  • Think about data flow

  • Refactor regularly

Mistakes are normal — repeating them without learning is the real issue.


Conclusion

React JS mistakes don’t disappear after the beginner stage. As applications grow, new types of mistakes emerge — often related to architecture, performance, and maintainability.

By recognizing these patterns early, you can build React applications that scale well, perform better, and remain easier to maintain over time.

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...