From the course: Android Compose with Kotlin

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Understand state hoisting

Understand state hoisting - Kotlin Tutorial

From the course: Android Compose with Kotlin

Understand state hoisting

- [Instructor] Composables are either stateful or stateless. The app composable in this example is stateful. By using the remember function it contains its own state. This makes composables less reusable and more challenging to test. While this version of the app composable is stateless. The value of the selectedSession is passed to the composable as a parameter. So, there's no need to contain any state. Stateless composables are preferred when creating your UI in Compose. Moving state from a child composable to a parent composable is known as state hoisting. Think lifting the state up from a child to a parent. This is one way to make a composable stateless. Let's modify our sample app to hoist the state properly. Over here in Android Studio we have a Column composable with one tag, and that tag has the string Android Basics. Let's see what happens when we put our app in Interactive Mode. So, we'll come here, we'll start Interactive Mode, and then we'll click on the tag. And we see…

Contents