Create a Message
component that takes a text
prop and displays it inside a <p>
element.
// Message.js
import React from "react";
const Message = (props) => {
return <p>{props.text}</p>;
In TypeScript, basic types like number, string, and boolean can be explicitly declared.
const a: number = 0;
This task involves creating a function with typed parameters and testing the behavior when the wrong type is passed.
Example:
Inspired by Tutorial
@Composable | |
fun <T> T.useDebounce( | |
delayMillis: Long = 300L, | |
// 1. couroutine scope | |
coroutineScope: CoroutineScope = rememberCoroutineScope(), | |
onChange: (T) -> Unit | |
): T{ | |
// 2. updating state | |
val state by rememberUpdatedState(this) | |
@Composable | |
fun RestaurantSearchBar( | |
modifier: Modifier = Modifier, | |
hint: String = "Search", | |
onSearch: (String) -> Unit = {} | |
) { | |
// 1. text field state | |
var text by remember { | |
mutableStateOf("") | |
} |
fun MutableList<Restaurant>.search( | |
query: String | |
): MutableList<Restaurant>{ | |
val result = mutableListOf<Restaurant>() | |
this.forEach {restaurant -> | |
if (restaurant.title.contains(query, ignoreCase = true) || restaurant.description.contains(query, ignoreCase = true)){ | |
result.add(restaurant) | |
} | |
} | |
return result |
class RestaurantsViewModel: ViewModel() { | |
private val _restaurantsState by mutableStateOf( | |
restaurants | |
) | |
val restaurantsState = _restaurantsState | |
fun toggleIsFavourite( | |
rid: Int, | |
) { |
@Composable | |
fun RestaurantScreen( | |
modifier: Modifier = Modifier | |
){ | |
val vm: RestaurantsViewModel = viewModel() | |
var myRestaurants = vm.restaurantsState | |