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

How React JS Works: Components, State, and Props Explained Simply


Introduction

After understanding why developers use React JS, the next important step is learning how React actually works. Many beginners feel confused by terms like components, state, and props — even though these are the core concepts of React.

In this article, we’ll break down how React JS works, using simple explanations and clear examples, without unnecessary complexity.


How React JS Thinks About the UI

Traditional websites update the page by reloading everything. React takes a different approach.

React treats the user interface as:

A collection of small, reusable pieces called components

Each component controls:

  • What is displayed

  • How it behaves

  • When it updates

When data changes, React automatically updates the affected parts of the UI.


What Are Components in React?

A component is a reusable piece of the interface.

Examples of components:

  • Button

  • Navbar

  • Card

  • Form

  • Footer

Instead of writing HTML repeatedly, React lets you build once and reuse.


Example of a Simple React Component

function Welcome() { return <h1>Welcome to React</h1>; }

This component:

  • Is written in JavaScript

  • Returns UI using JSX

  • Can be reused anywhere in the app


Why Components Are Important

Components help developers:

  • Organize code better

  • Reuse UI logic

  • Maintain large applications

  • Work efficiently in teams

In large projects, hundreds of components work together to form one application.


What Is JSX?

JSX looks like HTML but works inside JavaScript.

Example:

const title = "Hello React"; return <h1>{title}</h1>;

JSX allows you to:

  • Combine logic and UI

  • Use JavaScript expressions inside markup

  • Write cleaner, more readable code

Although JSX is optional, it is widely used because it improves developer experience.


What Is State in React?

State represents data that can change over time.

Examples of state:

  • User input

  • Button clicks

  • Toggle values

  • API responses

When state changes, React automatically re-renders the component.


Simple State Example

import { useState } from "react"; function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Clicked {count} times </button> ); }

What happens here:

  • count stores the current value

  • setCount updates the value

  • UI updates instantly when clicked


Why State Matters

Without state:

  • UI would be static

  • Interactions would feel slow or broken

State is what makes React applications interactive and dynamic.


What Are Props in React?

Props (short for properties) are used to pass data from a parent component to a child component.

Props are:

  • Read-only

  • Passed like function arguments

  • Used to customize components


Example of Props

function Greeting(props) { return <h2>Hello, {props.name}</h2>; } function App() { return <Greeting name="Faris" />; }

Here:

  • name is a prop

  • The Greeting component displays dynamic data


Difference Between State and Props

FeatureStateProps
Changes over timeYesNo
Owned by componentYesNo
Passed to childYesYes
Controls UI updatesYesNo

Understanding this difference is crucial when learning React.


How React Updates the UI

When something changes:

  1. State is updated

  2. React updates the Virtual DOM

  3. React compares changes

  4. Only the necessary parts are updated in the browser

This process keeps React applications fast and efficient.


Why These Concepts Matter Together

Components, state, and props work together to:

  • Keep code modular

  • Manage data flow clearly

  • Make UI predictable

Most React patterns and libraries are built on top of these core ideas.


Common Beginner Mistakes

Many beginners struggle because they:

  • Try to modify props directly

  • Store too much data in state

  • Create overly large components

Learning these basics early helps avoid bad habits later.


When You Truly Understand React

You know React well when you can:

  • Break UI into components

  • Decide what should be state

  • Pass data cleanly using props

  • Predict how UI will update

Once these fundamentals click, learning advanced React becomes much easier.


Conclusion

React JS is not magic — it’s a structured way of managing UI using components, state, and props.

By understanding how these three concepts work together, you gain a strong foundation to build scalable, maintainable, and interactive web applications.

This knowledge is essential before moving on to topics like routing, hooks, and performance optimization.

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