SPEAKER NOTES:
Today is demo-heavy. We’ll make the scorecard interactive using state.
Connect to Hope Foundation: a scorecard is useless if the strokes reset. State fixes that.
SPEAKER NOTES:
We’ll define state and show what goes wrong without `remember`.
SPEAKER NOTES:
We’ll explore multiple state variables, data class state, and lists.
SPEAKER NOTES:
We’ll refactor a component to be stateless and reusable.
SPEAKER NOTES:
State is one of the most important concepts in Compose. Let's break it down.
SPEAKER NOTES:
Examples: text input, checkbox, counter, selected item.
Relate to golf: current hole, strokes, selected team.
SPEAKER NOTES:
Compose automatically updates UI when the state changes.
SPEAKER NOTES:
Explain recomposition: every time Compose redraws, `strokes` resets to 0.
SPEAKER NOTES:
`remember` keeps state across recompositions. `mutableStateOf` makes it observable.
Explain Kotlin `by` delegation briefly.
SPEAKER NOTES:
Understanding recomposition is key to mastering Compose. Let's explore it.
SPEAKER NOTES:
Only affected composables re-run, not the entire screen.
SPEAKER NOTES:
This is normal. Don’t do side effects inside composables.
SPEAKER NOTES:
Show the syntactic convenience of `by` and `getValue`/`setValue`.
SPEAKER NOTES:
This is a realistic score-entry component: hole, strokes, confirmation.
SPEAKER NOTES:
Data classes keep related state together and make updates explicit via `copy`.
SPEAKER NOTES:
Derived state is computed from other state. No need to store it separately.
SPEAKER NOTES:
Key idea: create a new list instance so Compose detects the change.
SPEAKER NOTES:
State is trapped inside the composable. Parent can’t reset or coordinate totals.
SPEAKER NOTES:
Now the composable is stateless and reusable. State lives in the parent.
SPEAKER NOTES:
The parent owns the single source of truth and can calculate totals easily.
SPEAKER NOTES:
State hoisting is the key pattern for building reusable Compose components.
SPEAKER NOTES:
Data goes from parent to child. This is the Compose pattern.
SPEAKER NOTES:
Child emits events through callbacks. Parent updates state.
SPEAKER NOTES:
Stateless composables are easier to test and reuse.
SPEAKER NOTES:
Let's look at the concrete benefits of hoisting state to the parent.
SPEAKER NOTES:
Same component works for each hole.
SPEAKER NOTES:
We can pass in sample values without creating state.
SPEAKER NOTES:
Now you can add “Clear All Scores” or submit logic from the parent.
SPEAKER NOTES:
Not all state needs to be hoisted. Let's learn when and when not to.
SPEAKER NOTES:
If multiple components rely on the same data, hoist it.
SPEAKER NOTES:
If the parent should coordinate behavior, hoist the state.
SPEAKER NOTES:
Keep small internal state local when it doesn’t affect other components.
SPEAKER NOTES:
We’ll see why hoisting makes the team selector reusable across screens.
SPEAKER NOTES:
State is inside the component—hard to coordinate with the rest of the app.
SPEAKER NOTES:
Now it’s stateless, reusable, and testable.
SPEAKER NOTES:
The parent can now coordinate the score entry screen and selected team.
SPEAKER NOTES:
Let's summarize the key takeaways from state management in Compose.
SPEAKER NOTES:
Without state, the UI resets. With state, it feels like a real app.
SPEAKER NOTES:
Compute totals from existing state rather than storing duplicates.
SPEAKER NOTES:
This prepares us for larger screens where many components share data.