ถ้าคุณเป็นสาย React และกำลังใช้ Next.js เพื่อสร้างเว็บแอป คุณอาจสงสัยว่า "การสร้าง Component และนำไปใช้ใน Next.js ต่างจาก React ปกติยังไง?"
จริง ๆ แล้วหลักการสร้าง Component ใน Next.js ก็คล้ายกับ React ทั่วไป แต่ Next.js มีฟีเจอร์พิเศษบางอย่างที่ช่วยให้การจัดการ Component มีประสิทธิภาพขึ้น
บทความนี้จะพาคุณไปดูทุกมุมของการสร้าง Component ตั้งแต่พื้นฐาน ไปจนถึงแนวทางปฏิบัติที่ดี เพื่อให้คุณสามารถ เขียนโค้ดที่เป็นระเบียบ ใช้ซ้ำได้ และโหลดเร็วขึ้น
Component คืออะไร?
Component เป็นส่วนย่อยของ UI ที่สามารถนำมาใช้ซ้ำได้ ตัวอย่างเช่น
- ปุ่ม (Button)
- แถบนำทาง (Navbar)
- การ์ดแสดงข้อมูล (Card)
- กล่องข้อความ (Input field)
แทนที่เราจะเขียนโค้ดซ้ำ ๆ ทุกหน้า เราสามารถสร้าง Component และนำไปใช้ซ้ำได้ทั่วทั้งเว็บ
1. การสร้าง Component ใน Next.js
ใน Next.js ไฟล์ Component ควรเก็บไว้ในโฟลเดอร์ components/ เพื่อให้โครงสร้างโปรเจกต์ดูเป็นระเบียบ
ตัวอย่างการสร้าง Component Button.js
export default function Button({ text }) {
return <button className="btn">{text}</button>;
}
Note:
- เราส่งค่า text ผ่าน Props เพื่อให้ปุ่มนี้สามารถแสดงข้อความที่แตกต่างกันได้
- ควรตั้งชื่อไฟล์เป็น ตัวพิมพ์ใหญ่ เช่น Button.js ตามหลักการตั้งชื่อ Component
เมื่อสร้าง Component แล้ว เราสามารถ import และใช้มันในไฟล์อื่น ได้ เช่นใน pages/index.js
ใช้ Button ในหน้า index.js
import Button from '../components/Button';
export default function Home() {
return (
<div>
<h1>Welcome to Next.js 🚀</h1>
<Button text="คลิกเลย!" />
<Button text="สมัครสมาชิก" />
</div>
);
}
✅ ผลลัพธ์ที่ได้:
Welcome to Next.js 🚀
[ คลิกเลย! ] [ สมัครสมาชิก ]
Props เป็นค่าที่ส่งจาก Parent Component ไปยัง Child Component
ทำให้เราสามารถปรับแต่ง Component ได้ง่ายขึ้น
ตัวอย่าง Component Card.js รับค่า Props
export default function Card({ title, description }) {
return (
<div className="card">
<h2>{title}</h2>
<p>{description}</p>
</div>
);
}
การใช้ Card ใน index.js
import Card from '../components/Card';
export default function Home() {
return (
<div>
<h1>รายการบทความ</h1>
<Card title="Next.js คืออะไร?" description="Next.js เป็น React Framework ที่ได้รับความนิยมมาก" />
<Card title="การใช้ API Routes" description="API Routes ช่วยให้เราสร้าง Backend ใน Next.js ได้ง่าย" />
</div>
);
}
✅ ผลลัพธ์ที่ได้:
รายการบทความ
----------------
Next.js คืออะไร?
Next.js เป็น React Framework ที่ได้รับความนิยมมาก
การใช้ API Routes
API Routes ช่วยให้เราสร้าง Backend ใน Next.js ได้ง่าย
สังเกต:
- เราสามารถใช้ Card ได้หลายครั้งโดยเปลี่ยนค่า Props
- ทำให้โค้ดดูสะอาด และจัดการง่ายขึ้น
2. การจัดการ Layout ด้วย Component
Next.js มีไฟล์ _app.js ซึ่งช่วยให้เราสร้าง Layout ที่ใช้ซ้ำกันทุกหน้า ได้ง่ายขึ้น
ไฟล์ components/Layout.js
export default function Layout({ children }) {
return (
<div>
<header>🔥 My Website</header>
<main>{children}</main>
<footer>⚡ Powered by Next.js</footer>
</div>
);
}
import Layout from '../components/Layout';
import '../styles/globals.css';
export default function MyApp({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
}
✅ ผลลัพธ์ที่ได้
- ทุกหน้าจะมี Header และ Footer โดยอัตโนมัติ
3. การใช้ CSS ใน Component
Next.js รองรับการใช้ CSS ได้หลายแบบ เช่น
- Global CSS (styles/globals.css)
- Module CSS (.module.css)
- Styled Components
ไฟล์: styles/globals.css
.btn {
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
ไฟล์: components/Button.js
import '../styles/globals.css';
export default function Button({ text }) {
return <button className="btn">{text}</button>;
}
✅ ข้อดีของ Global CSS
- ใช้งานง่าย และใช้ได้กับทุก Component
ไฟล์: components/Button.module.css
.btn {
background-color: red;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
ไฟล์: components/Button.js
import styles from './Button.module.css';
export default function Button({ text }) {
return <button className={styles.btn}>{text}</button>;
}
✅ ข้อดีของ Module CSS
- ป้องกันการชนกันของ CSS
- ทำให้โค้ดเป็นระเบียบ
สรุป
| หัวข้อ | วิธีใช้งาน |
|---|---|
| สร้าง Component | ใช้ Function Component (export default function Component() {}) |
| ใช้ Component | import Component from '../components/Component' |
| ส่งค่าเข้า Component | ใช้ Props (<Component name="John" />) |
| ใช้ Layout ซ้ำกันทุกหน้า | ใช้ _app.js และ Layout Component |
| ใช้ CSS ใน Component | ใช้ Global CSS หรือ Module CSS |
ข้อดีของการใช้ Component ใน Next.js
✅ โค้ดสะอาดขึ้น
✅ ใช้ซ้ำได้
✅ โหลดเร็วขึ้น (Static Optimization)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น