This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyComponent() { | |
| val count = remember { mutableStateOf(0) } | |
| val countFlow = snapshotFlow { count.value } | |
| LaunchedEffect(countFlow) { | |
| countFlow.collect { value -> | |
| // Handle the new value | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyComponent() { | |
| var email by remember { mutableStateOf("") } | |
| val isValidEmail = remember { | |
| derivedStateOf { | |
| isEmailValid(email) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun loadNetworkImage( | |
| url: String, | |
| imageRepository: ImageRepository = ImageRepository() | |
| ): State<Result<Image>> { | |
| // Creates a State<T> with Result.Loading as initial value | |
| // If either `url` or `imageRepository` changes, the running producer | |
| // will cancel and will be re-launched with the new inputs. | |
| return produceState<Result<Image>>(initialValue = Result.Loading, url, imageRepository) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Ref(var value: Int) | |
| @Composable | |
| inline fun LogCompositions(tag: String) { | |
| val ref = remember { Ref(0) } | |
| SideEffect { ref.value++ } | |
| Logger.log("$tag Compositions: ${ref.value}") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyComponent() { | |
| var data by remember { mutableStateOf("") } | |
| val disposableEffect = remember { mutableStateOf<Disposable?>(null) } | |
| DisposableEffect(Unit) { | |
| val disposable = someAsyncOperation().subscribe { | |
| data = it | |
| } | |
| onDispose { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun ParentComponent() { | |
| setContent { | |
| ComposeTheme { | |
| // A surface container using the 'background' color from the theme | |
| Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) { | |
| var dynamicData by remember { | |
| mutableStateOf("") | |
| } | |
| LaunchedEffect(Unit) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyComponent() { | |
| val coroutineScope = rememberCoroutineScope() | |
| val data = remember { mutableStateOf("") } | |
| Button(onClick = { | |
| coroutineScope.launch { | |
| // Simulate network call | |
| delay(2000) | |
| data.value = "Data loaded" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Launched Effect | |
| private var i = 0 | |
| @Composable | |
| fun SideEffect() { | |
| var text by remember { | |
| mutableStateOF("") | |
| } | |
| LaunchedEffect(key1 = text) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Side Effect | |
| private var i = 0 | |
| @Composable | |
| fun SideEffect() { | |
| var text by remember { | |
| mutableStateOF("") | |
| } | |
| Column { | |
| Button(onClick = { text += "@" }) { |