Skip to main content

Components and Props

In this section of the tutorial, you'll learn the basic Litho building blocks then create your first component that uses props.

Basic Litho building blocks

Basic Terminology
  • Component - all user-interactable elements in the UI (such as buttons, checkboxes, scrollbars, lists, menus, and text fields) that you see in the application are components. To be used, a component must be placed in a container. For more information, see the Components page in the 'Main Concepts' section.
  • Container component - arranges groups of components in a layout.
  • Prop - an item of data that cannot be changed (making it 'immutable') during the associated component's lifecycle. For more information, see the Types of Props page in the 'Main Concepts' section.

To display the classic "Hello, World!" text on the screen with Litho, you need to integrate the Litho component hierarchy into your View hierarchy. To illustrate this, the "Hello, World" code (MyActivity.kt), from the Setting up the Project section of the tutorial, is shown below:

import android.os.Bundle
import com.facebook.litho.Component
import com.facebook.litho.ComponentScope
import com.facebook.litho.KComponent
import com.facebook.litho.LithoView
import com.facebook.litho.dp
import com.facebook.litho.kotlin.widget.Text

class MyActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

val lithoView = LithoView.create(this /* context */, MyComponent())

class MyComponent() : KComponent() {
override fun ComponentScope.render(): Component = Text(text = "Hello, World!", textSize = 50.dp)

Key Points in MyActivity.kt

  • LithoView - a hierarchy of Litho components is rendered using a LithoView.
  • val - holds an immutable object that cannot be changed during the lifecycle of the component.
  • Text(...) - the 'Text' component (this is how you create an instance of a component (both built-in and those you define yourself).
  • Text(...) - assigns values to the props text and textsize.

Create your first component

Previously, you used a built-in Text component. Now, you'll define your own using the following 'HelloComponent.kt' code. As with the above, your 'first' component can also declare props:

class HelloComponent(private val name: String) : KComponent() {

override fun ComponentScope.render(): Component? {
return Text(text = "Hello $name!")

Key Points in HelloComponent.kt

  • KComponent - a class needed to extend in order to create components.
  • name - a String prop named name.
  • render - function override that returns what your component should render.
  • Text(...) - returns an instance of the Text component with its text prop set to the String "Hello $name'.

Lots of code autocompletion and class templates can be found in the Litho Android Studio plugin!

Use you first component

To use your component, just replace the Text component in the "Hello, World!" example with an instance of your HelloComponent:

setContentView(LithoView.create(this, HelloComponent(name = "Linda")))

What next?

The next section of the tutorial Introducing Layout helps you become familiar with building layouts using Flexbox.