แหล่งความจริงเพียงหนึ่งเดียว
หลีกเลี่ยงสถานะซ้ำซ้อนที่คลาดเคลื่อนจากกัน
แหล่งความจริงเพียงหนึ่งเดียว เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
One Place for Each Fact
A single source of truth means every piece of state lives in exactly one spot. Everything else reads from it, never copies it.
The Danger of Duplicates
If two Composables each hold their own copy of the same value, they can drift apart. One updates, the other shows stale data. 😬
How Hoisting Helps
Hoisting naturally gives you a single owner. The parent holds the value; children only receive it, so there is nothing to fall out of sync.
Read From the Source
Children should always read from the hoisted value, not from a private copy. When the source changes, every reader updates together.
var query by remember { mutableStateOf("") }
SearchBar(value = query, onValueChange = { query = it })
Results(query = query)Derive, Do Not Duplicate
Need a filtered or formatted version? Derive it from the source instead of storing a second copy that can go stale.
val matches = items.filter { it.contains(query) }derivedStateOf for Heavy Work
When a derived value is expensive, wrap it in derivedStateOf so it recomputes only when its inputs truly change.
val matches by remember {
derivedStateOf { items.filter { it.contains(query) } }
}Two Inputs, One State
Want two fields editing the same data? Point both at the same hoisted state. They share one truth and stay perfectly in sync.
Avoid the Mirror Trap
A common bug is copying a parameter into local state inside an effect. Now you have a mirror that silently goes out of date.
Lift, Do Not Mirror
If a child needs to change shared data, lift the state to a common parent instead of duplicating it. One owner, many readers.
Truth and Testing
A single source makes bugs obvious: there is only one place a value can be wrong, so you know exactly where to look. ✅
The Golden Rule
State should be owned once and shared by reference. Duplicate state is the root of most sync bugs in Compose.
Quick Check
Why is keeping a single source of truth important?
Recap
Keep each fact in one place, read or derive from it, and never mirror it. One source of truth means no drift. 🚀
คำถามที่พบบ่อย
บทเรียน “แหล่งความจริงเพียงหนึ่งเดียว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แหล่งความจริงเพียงหนึ่งเดียว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แหล่งความจริงเพียงหนึ่งเดียว”
หลีกเลี่ยงสถานะซ้ำซ้อนที่คลาดเคลื่อนจากกัน คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “แหล่งความจริงเพียงหนึ่งเดียว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Composable แบบมีสถานะกับไร้สถานะ
- ยกสถานะขึ้นด้วย value และ onValueChange
- แหล่งความจริงเพียงหนึ่งเดียว
- ช่องสถานะ Compose และแลมบ์ดาเนื้อหา