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β
- 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 androidx.appcompat.app.AppCompatActivity
import com.facebook.litho.Component
import com.facebook.litho.ComponentScope
import com.facebook.litho.KComponent
import com.facebook.litho.LithoView
import com.facebook.litho.kotlin.widget.Text
import com.facebook.rendercore.dp
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val lithoView = LithoView.create(this /* context */, MyComponent())
setContentView(lithoView)
}
}
class MyComponent() : KComponent() {
override fun ComponentScope.render(): Component {
return Text(text = "Hello, World!", textSize = 50.dp)
}
}
Key Points in MyActivity.ktβ
LithoView
- a hierarchy of Litho components is rendered using a LithoView.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 propstext
andtextsize
.
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.val name
- holds an immutableString
prop namedname
that cannot be changed during the lifecycle of the component.render
- function override that returns what your component should render.Text(...)
- returns an instance of theText
component with itstext
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.
For more information, see the Components and Props pages of the 'Main Concepts' section.