SwiftUI Academy · บทเรียน

หลีกเลี่ยงการวาดใหม่โดยไม่จำเป็น

จำกัดขอบเขตสถานะเพื่อลดการประเมิน body ใหม่

บทเรียน 2 จาก 413 ขั้นตอน

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

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

The Cost of Over-Updating

If a tiny change forces a huge view to rebuild, scrolling stutters. The fix is to keep each redraw as small as possible.

State Lives Where It Changes

Put @State in the smallest view that owns it. Then only that little view re-evaluates when the value flips, not its big parent.

struct Counter: View {
    @State private var count = 0
}

Extract Subviews

Break a large body into smaller subviews. SwiftUI can then diff and skip the pieces whose inputs did not change.

var body: some View {
    HeaderView()
    FeedView()
}

Pass Only What You Need

Hand a subview just the values it reads. The fewer dependencies a view has, the less often it must recompute.

RowView(title: item.title)

Watch Observable Reads

With @Observable models, a view updates only for the exact properties it touches. Read just the fields you display.

Avoid Wide State Objects

One giant model read everywhere makes every change ripple outward. Split it so each view depends on a narrow slice of data.

Beware Closures Capturing Self

A closure that reads many properties widens a view's dependencies. Capture only the specific value the action truly needs.

Move Heavy Work Out of body

Never do expensive computing inside body. It runs often, so cache results in state or a model and read the finished value.

Use let for Constants

Mark unchanging inputs as let. Constant properties never trigger updates, signaling clearly that they cannot cause a redraw.

struct Badge: View {
    let label: String
}

Equatable Views

For costly subviews, make them Equatable so SwiftUI can compare inputs and skip re-evaluation when they match.

ExpensiveView(data: data)
    .equatable()

Optimize What You Measure

Do not guess. Scope state, extract views, then measure to confirm you actually shrank the redraws that hurt.

Quick Check

Where should you place state to limit redraws?

Recap: Avoiding Redraws

You learned to scope state tightly, extract subviews, pass minimal data, and keep body cheap. Smaller dependencies mean fewer redraws. ⚡

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

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

บทเรียน “หลีกเลี่ยงการวาดใหม่โดยไม่จำเป็น” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “หลีกเลี่ยงการวาดใหม่โดยไม่จำเป็น”

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

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

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

บทเรียน “หลีกเลี่ยงการวาดใหม่โดยไม่จำเป็น” ใช้เวลานานแค่ไหน

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

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

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

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

  1. วิธีที่ SwiftUI คำนวณมุมมองใหม่
  2. หลีกเลี่ยงการวาดใหม่โดยไม่จำเป็น
  3. โหลดเนื้อหาหนักแบบขี้เกียจ
  4. วิเคราะห์ประสิทธิภาพด้วย Instruments
← กลับไปที่ SwiftUI Academy