0Pricing
Android Academy · Lesson

State Hoisting

Lift state up for reusable composables.

State Hoisting is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is State Hoisting?

State hoisting means moving state up out of a composable to its caller. The composable becomes stateless: it just receives a value and reports changes.

Stateful vs Stateless

A stateful composable owns its state with remember. A stateless one holds no state; it takes the value as a parameter and exposes an event. Stateless composables are easier to reuse and test.

The value + onValueChange Pattern

The standard hoisting shape has two parameters: a value (current state, passed down) and an onValueChange lambda (event, passed up).

@Composable
fun NameField(value: String, onValueChange: (String) -> Unit) {
    TextField(value = value, onValueChange = onValueChange)
}

Before Hoisting

Here the composable owns its state. It works, but the parent has no access to the text and the composable is harder to reuse.

@Composable
fun NameField() {
    var name by remember { mutableStateOf("") }
    TextField(value = name, onValueChange = { name = it })
}

After Hoisting

Now state lives in the parent. The field is stateless and reusable; the parent controls the value.

@Composable
fun Parent() {
    var name by remember { mutableStateOf("") }
    NameField(value = name, onValueChange = { name = it })
}

Down and Up

The pattern is state flows down, events flow up. The value travels down as a parameter; changes travel up through the callback. This is called unidirectional data flow.

Why Hoist?

Hoisting lets multiple composables share the same state, lets the parent react to changes, and makes the child a pure function of its inputs — perfect for previews and tests.

Where to Hoist

Hoist state to the lowest common ancestor that needs it. If only one composable uses the state, hoisting may be unnecessary. Hoist when something above needs to read or control it.

A Shared Example

Here a single piece of state drives both a field and a live preview, only possible because the state is hoisted.

@Composable
fun Editor() {
    var text by remember { mutableStateOf("") }
    Column {
        TextField(value = text, onValueChange = { text = it })
        Text(text = "Preview: $text")
    }
}

Stateless Is Reusable

Because a stateless composable takes its value as a parameter, you can preview it with sample data and use it anywhere with different state owners.

@Preview
@Composable
fun NameFieldPreview() {
    NameField(value = "Sample", onValueChange = {})
}

Putting It Together

A reusable counter where the parent owns the count and the button is stateless.

@Composable
fun CounterButton(count: Int, onIncrement: () -> Unit) {
    Button(onClick = onIncrement) {
        Text(text = "Count: $count")
    }
}

@Composable
fun Screen() {
    var count by remember { mutableStateOf(0) }
    CounterButton(count = count, onIncrement = { count++ })
}

Quick Check

In state hoisting, what two things does the stateless composable typically expose?

Recap

State hoisting:

  • Move state up to the caller; make the child stateless.
  • Use the value + onValueChange pattern.
  • State down, events up — unidirectional data flow.
  • Hoist to the lowest common ancestor that needs the state.

Frequently asked questions

Is the “State Hoisting” lesson free?

Yes — the full text of “State Hoisting” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “State Hoisting”?

Lift state up for reusable composables. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “State Hoisting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. State with remember and mutableStateOf
  2. Recomposition Explained
  3. State Hoisting
  4. rememberSaveable
← Back to Android Academy