ถ้าพูดถึงการจัดการ state ใน Next.js เชื่อว่าหลายคนต้องเคยลังเลระหว่าง Zustand กับ Redux แน่นอน เพราะทั้งสองตัวนี้ได้รับความนิยมมากและมีแนวทางการทำงานที่แตกต่างกัน วันนี้เราจะมาคุยกันแบบบ้านๆ ไม่เน้นทฤษฎีเยอะ แต่เน้นให้เข้าใจง่าย ว่าควรเลือกใช้ตัวไหนดีสำหรับโปรเจกต์ของคุณ
ทำไมต้องจัดการ State ใน Next.js?
Next.js เป็น React Framework ที่ช่วยให้เราสร้างเว็บแอปพลิเคชันได้เร็วและดีขึ้น แต่ถึงจะเทพแค่ไหน Next.js ก็ยังมีปัญหาเรื่องการจัดการ state ที่เราต้องคิดหนัก โดยเฉพาะเมื่อเว็บของเรามีหลายหน้า (multi-page) หรือมีข้อมูลที่แชร์กันระหว่างคอมโพเนนต์หลายตัว
ในกรณีที่ state ของเราซับซ้อนขึ้น การใช้ useState() หรือ useContext() อาจจะไม่พอแล้ว เช่น
- มีหลายคอมโพเนนต์ต้องใช้ข้อมูลร่วมกัน
- ต้องเก็บสถานะของผู้ใช้ที่ล็อกอิน
- ต้องดึงข้อมูลจาก API แล้วแชร์ให้ทุกหน้าใช้
ถึงจุดนี้แหละ ที่พวก State Management Library อย่าง Zustand และ Redux โผล่มาช่วยชีวิตเรา
Redux: เจ้าพ่อ State Management
จุดเด่นของ Redux
Redux เป็น state container ระดับตำนานที่อยู่กับ React มานาน มันใช้ single source of truth (เก็บ state ไว้ที่เดียว) และเปลี่ยนค่า state ด้วย action และ reducer
สิ่งที่ทำให้ Redux เจ๋งคือ
- Predictable State – ทุกการเปลี่ยนแปลงของ state ถูกควบคุมโดย action และ reducer ทำให้ debug ง่าย
- DevTools สุดเทพ – มี Redux DevTools ให้ตรวจสอบทุกการเปลี่ยนแปลงของ state ได้แบบ real-time
- Ecosystem ใหญ่โต – มี middleware อย่าง Redux Thunk หรือ Redux Saga ช่วยให้จัดการ asynchronous action ได้ง่ายขึ้น
จุดด้อยของ Redux
- โค้ดยาว – Redux ต้องสร้าง action, reducer และ dispatch ซึ่งทำให้โค้ดยืดยาว ถ้าทำผิดที่เดียวอาจล้มทั้งระบบ
- ต้องใช้ boilerplate เยอะ – แค่จะอัพเดท state ต้องผ่านหลายขั้นตอน เช่น สร้าง action → dispatch action → reducer → update store
- Learning Curve สูง – สำหรับมือใหม่ Redux อาจทำให้ปวดหัว เพราะต้องเข้าใจโครงสร้างหลายอย่าง
Zustand: เบา สั้น และเร็ว
จุดเด่นของ Zustand
Zustand เป็น state management ที่เบาและง่ายกว่า Redux มันใช้แนวคิดของ flux pattern แต่ไม่ต้องมี action หรือ reducer ให้วุ่นวาย
สิ่งที่ทำให้ Zustand น่าสนใจคือ
- API ใช้ง่าย – ไม่ต้องมี action หรือ reducer แค่สร้าง store แล้วใช้ได้เลย
- ไม่มี boilerplate – Zustand ใช้ฟังก์ชันเดียวสร้าง state ได้ทันที
- ใช้งานร่วมกับ Server Components ได้ดี – ทำให้เหมาะกับ Next.js ที่ใช้ App Router
จุดด้อยของ Zustand
- ไม่มี DevTools ที่เทพเท่า Redux – ถ้าต้องการ debug อาจต้องใช้ console.log แทน
- เหมาะกับโปรเจกต์ขนาดกลาง-เล็ก – ถ้าเป็นแอปขนาดใหญ่ Redux อาจจะควบคุม state ได้ดีกว่า
เปรียบเทียบ Redux vs Zustand
| Feature | Redux | Zustand |
|---|---|---|
| โครงสร้างโค้ด | ซับซ้อน ต้องมี action, reducer, store | เรียบง่าย ใช้ฟังก์ชันเดียวสร้าง store |
| Performance | อาจมี overhead สูงถ้าใช้ไม่ดี | เบาและเร็วกว่า Redux |
| DevTools | Redux DevTools ช่วย debug ได้ดี | มี DevTools แต่ไม่เทพเท่า Redux |
| การใช้งานกับ Next.js | ต้อง config เพิ่ม | ใช้งานง่าย ไม่มี config ยุ่งยาก |
| เหมาะกับโปรเจกต์ | ใหญ่และซับซ้อน | เล็ก-กลาง |
ควรใช้ Zustand หรือ Redux ใน Next.js?
ใช้ Redux ถ้า
✅ โปรเจกต์ของคุณ ซับซ้อน และต้องจัดการ state เยอะ
✅ ต้องการ DevTools เพื่อ debug state ได้ง่าย
✅ แอปของคุณต้องการ middleware เช่น Redux Thunk หรือ Redux Saga
ใช้ Zustand ถ้า
✅ คุณต้องการโค้ดที่ เรียบง่าย และ ไม่ต้องการ boilerplate
✅ โปรเจกต์ของคุณเป็น ขนาดเล็ก-กลาง และไม่ต้องการ state management ที่หนัก
✅ คุณใช้ Next.js App Router และต้องการ state management ที่เข้ากันได้ดี
ตัวอย่างการใช้ Redux กับ Next.js
ก่อนจะใช้ Redux กับ Next.js ต้องติดตั้งแพ็กเกจก่อน
npm install @reduxjs/toolkit react-redux
จากนั้นสร้าง store.js
import { configureStore, createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1; },
decrement: (state) => { state.value -= 1; },
},
});
export const { increment, decrement } = counterSlice.actions;
export const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
});
แล้วใช้ Provider ใน _app.js หรือ layout.js
import { Provider } from 'react-redux';
import { store } from '../store';
export default function App({ Component, pageProps }) {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
);
}
ดึงค่า state ไปใช้ในคอมโพเนนต์
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from '../store';
export default function Counter() {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();
return (
<div>
<h1>{count}</h1>
<button onClick={() => dispatch(increment())}>+</button>
<button onClick={() => dispatch(decrement())}>-</button>
</div>
);
}
ตัวอย่างการใช้ Zustand กับ Next.js
ติดตั้ง Zustand ก่อน
npm install zustand
สร้าง store.js
import { create } from 'zustand';
const useCounterStore = create((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
}));
export default useCounterStore;
ใช้ในคอมโพเนนต์
import useCounterStore from '../store';
export default function Counter() {
const { count, increment, decrement } = useCounterStore();
return (
<div>
<h1>{count}</h1>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
</div>
);
}
สรุป
- Redux เหมาะกับโปรเจกต์ขนาดใหญ่ที่ต้องการความเป็นระบบ
- Zustand เหมาะกับโปรเจกต์ที่ต้องการความง่ายและเร็ว
- ถ้าคุณไม่แน่ใจ ให้ลองใช้ Zustand ก่อน ถ้ารู้สึกว่าไม่พอ ค่อยย้ายไป Redux
สุดท้ายแล้ว การเลือกเครื่องมือขึ้นอยู่กับ ความเหมาะสมของโปรเจกต์ ไม่มีกฎตายตัวว่าต้องใช้ตัวไหนเสมอไป
ไม่มีความคิดเห็น:
แสดงความคิดเห็น