Component

abstract class Component
kotlin.Any
   ↳ androidx.compose.Component

A Component is a class variant of a Composable function. Components must implement the compose method, which behaves like the body of the function. Component instances are preserved across compositions, much like local state in Composable functions that is introduced using state or memo.

There is nothing that Component classes offer that Composable functions don't also offer, so we currently recommend against using Component for most use cases.

Component can map to concepts of Composable functions, but also have a few restrictions that most classes do not have. For example:

  1. Private properties of component classes are similar to local state of a composable function.
  2. Non-Private properties of component classes are similar to parameters of Composable functions
  3. Constructor parameters of component classes are similar to Pivotal parameters of Composable functions
  4. You should only read non-private properties. Never set them.

A simple "counter" Component using private properties as local state:

    // Definition
    class Counter : Component {
      private var count = 0
      override fun compose() {
          Button(text="Increment", onClick={ count += 1; recompose() })
          Button(text="Decrement", onClick={ count -= 1; recompose() })
          Text(text="Counter: $count")
      }


    // Usage:
    Counter()

Summary

Public constructors

A Component is a class variant of a Composable function.

Public methods

abstract Unit

The compose method is the only required method to implement in Component sub-classes.

Protected methods

Unit

Schedules this component to be recomposed.

Public constructors

<init>

Component()

A Component is a class variant of a Composable function. Components must implement the compose method, which behaves like the body of the function. Component instances are preserved across compositions, much like local state in Composable functions that is introduced using state or memo.

There is nothing that Component classes offer that Composable functions don't also offer, so we currently recommend against using Component for most use cases.

Component can map to concepts of Composable functions, but also have a few restrictions that most classes do not have. For example:

  1. Private properties of component classes are similar to local state of a composable function.
  2. Non-Private properties of component classes are similar to parameters of Composable functions
  3. Constructor parameters of component classes are similar to Pivotal parameters of Composable functions
  4. You should only read non-private properties. Never set them.

A simple "counter" Component using private properties as local state:

    // Definition
    class Counter : Component {
      private var count = 0
      override fun compose() {
          Button(text="Increment", onClick={ count += 1; recompose() })
          Button(text="Decrement", onClick={ count -= 1; recompose() })
          Text(text="Counter: $count")
      }


    // Usage:
    Counter()

See Also

Public methods

compose

@Composable abstract fun compose(): Unit

The compose method is the only required method to implement in Component sub-classes. The method is Composable, and the sub-hierarchy of the Component itself needs to be composed during the execution of this method.

The compose method should not mutate the component itself during its own execution (in other words, compose can be thought of as "pure").

The compose runtime calls compose, which means that the compose method should never be called directly.

Protected methods

recompose

protected fun recompose(): Unit

Schedules this component to be recomposed. If you have updated any state that is used during compose, you will likely want to call this to cause compose to get called again and have the tree updated.