01วาง State ให้ใกล้เจ้าของที่สุด
UI State
dialog, input draft, selected tab และค่าที่ component subtree เดียวใช้
Shared App State
theme, cart draft, permission snapshot หรือ workflow ข้ามหลาย route
Remote State
ข้อมูล API ที่มี loading, retry, cache, stale time และ invalidation
Server state ไม่ควรถูก copy เข้า global store โดยไม่มีเหตุผล เพราะจะเกิด source of truth สองชุดและต้องเขียน cache invalidation เอง ใช้ framework data layer หรือ query cache จัดการ lifecycle แล้วเก็บเฉพาะ client draft ที่ผู้ใช้กำลังแก้แยกออกมา
02Re-render ไม่ใช่ Bug เสมอไป
Render คือการคำนวณ UI ใหม่ ปัญหาเกิดเมื่อ render มีงานแพงหรือ subtree ใหญ่ถูก invalidate บ่อย ตรวจด้วย React Profiler หรือ Vue DevTools ก่อน แล้วแก้จาก state placement, context/store selector, object identity, expensive computation และ list virtualization
- ย้าย state ลงไปใกล้ component ที่ใช้ เพื่อลด blast radius
- แยก context ตาม update frequency และทำ provider value ให้ stable
- เลือก store slice ผ่าน selector แทน subscribe ทั้ง object
- memoize เมื่อ profiler ชี้ว่าคุ้ม ไม่ใส่ `memo`/`useMemo` ทุกจุด
03โจทย์และแนวคำตอบ
Local state, Global state และ Server state ต่างกันอย่างไร?
Local state มี owner และ lifecycle อยู่ใน component/subtree เช่น dialog หรือ form draft; global state คือ client state ที่หลายส่วนต้องอ่านหรือแก้ เช่น theme และ cart; server state เป็นสำเนาข้อมูลระยะไกลที่มี freshness, loading, retry และ cache invalidation
ผมวาง state ให้ใกล้ consumer ที่สุด ใช้ store เฉพาะข้อมูล client ที่แชร์จริง และใช้ server-state/query layer สำหรับข้อมูล API เพื่อไม่สร้าง source of truth ซ้ำ
เมื่อไหร่ควรใช้ Store เช่น Redux, Zustand หรือ Pinia และเมื่อไหร่ไม่จำเป็น?
ใช้เมื่อ state มี consumer หลาย subtree/route, update rule ซับซ้อน, ต้อง inspect action, persist หรือประสาน workflow ร่วมกันครับ ถ้าเป็น state ของ component เดียวหรือส่งผ่าน parent ใกล้ ๆ ได้ local state/composition มักชัดกว่า
Store ไม่ควรเป็นที่ทิ้งทุก API response และไม่แก้ prop drilling โดยอัตโนมัติ—มันแลกกับ global dependency, lifecycle ยาวขึ้น และต้องออกแบบ selector กับ reset ให้ดี
Prop drilling คืออะไร และมีวิธีแก้อย่างไร?
คือ component กลางต้องส่ง prop ที่ตัวเองไม่ใช้ต่อหลายชั้น แต่สองสามชั้นไม่ใช่ปัญหาเสมอ เพราะ data flow ยัง explicit ทางแก้มี component composition/children, ย้าย state ใกล้ consumer, context สำหรับ dependency ใน subtree หรือ store เมื่อแชร์กว้างจริง
ผมเลือกวิธีตามขอบเขตและความถี่การ update ไม่ย้ายทุกอย่างเข้า context เพราะ provider value เปลี่ยนอาจทำให้ consumer จำนวนมาก render ตาม
การใช้ state ไม่เหมาะสมทำให้ re-render เกินจำเป็นอย่างไร และแก้อย่างไร?
ตัวอย่างคือยก state สูงเกินไป, เก็บ derived state ซ้ำ, context value สร้าง object ใหม่ทุก render หรือ subscribe store ทั้งก้อน ทำให้การเปลี่ยนเล็กน้อย invalidate subtree ใหญ่
ผม profile ก่อน แล้ว colocate state, แยก context, ใช้ selector, รักษา identity ที่จำเป็น และ virtualize list ยาว จากนั้นวัด commit time อีกครั้ง ไม่ optimize จากจำนวน render เพียงอย่างเดียว
บน React และ Next.js ควรจัดการ State อย่างไร เมื่อไรใช้ React Context และเมื่อไรใช้ Redux หรือเครื่องมืออื่น?
ผมแยก ownership ก่อน: UI state ใช้ useState หรือ useReducer, filter/pagination ที่แชร์ลิงก์ได้เก็บใน URL, server state ให้ Server Component หรือ query cache ดูแล freshness ส่วน Context เหมาะกับ dependency/state ของ subtree ที่ขอบเขตชัด เช่น theme, locale หรือ feature workflow
Redux Toolkit เหมาะเมื่อ mutable client state ถูกใช้ข้ามหลาย feature, transition ซับซ้อน และต้องการ selector/middleware/DevTools บน Next.js App Router ต้องสร้าง store ต่อ request/client tree ผ่าน Client Provider และไม่ให้ Server Component อ่านหรือเขียน global store
อ่านเอกสารทางการ: Redux Toolkit Setup with Next.js ↗
อ่านเอกสารทางการ: Vue: State Management และ Pinia ↗