MA4: App architecture, state and storage on the phone

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

Where state lives

🎯You'll be able to

Decide whether each piece of state belongs to a component, to the app, to the phone's storage or to the server, and keep one source of truth for each.

🏠The everyday picture

A family keeps one shopping list on the fridge. If everyone kept their own list, someone would buy milk twice and nobody would buy rice.

📘What it really is

📝A worked example

Input: KhetNote's list and banner each count waiting changes their own way, and show different numbers.

Steps:

  1. Make the store's outbox the source of truth for waiting changes.
  2. Let the sync engine's status() read the outbox and report pending.
  3. Both the banner and any other screen read status(), so they agree.

Expected output: one number, shown the same everywhere.

🌍Where it's used

📝Case scenario

A shopping app kept the cart count in the header and the cart in the checkout screen separately. After an item was removed at checkout, the header still said 3. Reading both from one cart store ended the mismatch.

🖱️Try it

List five pieces of state in KhetNote (for example the form's fields, the visits, the outbox, the sync status, dark mode) and say where each belongs. Success: each has one owner.

⚠️Common mistakes

🛠️For your project

Project 1's banner message comes from status(), which reads the outbox: one source of truth.

✅Quick check

What is a "source of truth"?

Show the answer

The one place a piece of data lives, which everything else reads from.

Where should the half-typed value of a form field live?

Show the answer

In that screen's component state (saved as a draft if it must survive interruptions).

← PreviousOrganising app code
ProLearners Assist