0Pricing
Jetpack Compose Academy · บทเรียน

viewModel() ใน Composable

รับและกำหนดขอบเขตของ ViewModel อย่างถูกต้อง

viewModel() ใน Composable เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Getting a ViewModel

Inside a Composable you don’t construct a ViewModel with new. Instead you call the viewModel() function to fetch the right instance.

val vm: CounterViewModel = viewModel()

Where It Comes From

The viewModel() helper looks up the nearest lifecycle owner and returns the ViewModel scoped to it, creating one only if none exists yet.

Same Instance on Recomposition

Recomposition runs your function many times, yet viewModel() returns the same instance each time, so your state never resets unexpectedly.

Reading State From It

Once you hold the ViewModel, read its observable state and your Composable redraws whenever that state changes. The UI follows the data. 🔗

val vm: CounterViewModel = viewModel()
Text("Count: " + vm.count)

Calling Its Functions

Send user actions to the ViewModel by calling its functions from event lambdas, keeping the decision-making logic out of your Composable.

Button(onClick = { vm.increment() }) {
    Text("Add")
}

The Import

The function comes from the lifecycle Compose artifact. Import androidx.lifecycle.viewmodel.compose.viewModel to use it.

import androidx.lifecycle.viewmodel.compose.viewModel

Scoping by Default

By default the ViewModel is scoped to the host Activity or navigation destination, so all Composables in that scope share one instance. 🎯

Constructor Arguments

Need to pass dependencies? Provide a factory so the framework knows how to build a ViewModel that has constructor parameters.

val vm: CartViewModel = viewModel(factory = CartFactory(repo))

Hoist, Don’t Pass Down

Call viewModel() high up, near the screen root, then pass plain state and callbacks down to keep child Composables reusable.

Preview-Friendly Design

Because viewModel() needs a real owner, design children to take simple parameters so they still render in an isolated @Preview.

A Quick Pattern

The common flow is simple: obtain the ViewModel, read its state, and forward events to its functions. Three steps, every screen. ✨

Quick Check

Check how you obtain a ViewModel from a Composable.

Recap

Call viewModel() to fetch a scoped ViewModel, read its state to drive the UI, and forward user events to its functions instead of constructing it yourself.

คำถามที่พบบ่อย

บทเรียน “viewModel() ใน Composable” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “viewModel() ใน Composable” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “viewModel() ใน Composable”

รับและกำหนดขอบเขตของ ViewModel อย่างถูกต้อง คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “viewModel() ใน Composable” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใด ViewModel จึงคงอยู่เมื่อหมุนหน้าจอ
  2. viewModel() ใน Composable
  3. จำลองคลาสข้อมูล UiState
  4. collectAsStateWithLifecycle
← กลับไปที่ Jetpack Compose Academy