• Single Screen(Single Component) = Sum of Multiple Components • Function Component vs Class Component • Function Component: Using data from parameters or local variables • Jetpack Compose(Android), React(React Native) • Class Component: Using data from the constructor, member variables • SwiftUI, React(React Native, old fashioned)
So far, we have dealt with components using prede fi ned-data(parameter, constructor etc) • Then what should be done if the user needs to enter data or change the screen by user interactions?
“STATE” is the answer • Change of state triggers screen’s change -> re-render • Implementations of state and re-render in each libraries seem quite similar
How would you implement alert when like icon is displayed? • Although it is not directly related to rendering the UI, you must consider handling the operations necessary to prepare the data required for the UI. • Side Effect = Non-UI Rendering Related Logic, but ESSENTIAL Logics • HTTP Request, Permission Requests, Alarm Noti fi cation etc
keys: similar as “dependency array” • but if you want to execute only once when component mounts, you need to put unchangeable value (Unit, true) • Side Effect: de fi ne your side effect (suspend function) LaunchedEffect(key1, key2) { / / Side Effect }
keys: same as LaunchedEffect • Side Effect: non-suspend function • onDispose: executes when component unmounts DisposableEffect(key1, key2) { / / Side Effect onDispose { } }
knowledge, you should be able to create components or screens with simple logic using declarative UI. • However, if the logic becomes even slightly more complex or the screen has multiple layers, the readability & developer experience can signi fi cantly decrease.
the ui state or theme data(color, typography etc) to child components. • Using parameters (props drilling) • Local scoped data (like data teleportation)
in Jetpack Compose - CompositionLocal • staticCompositionLocalOf • Suppose CompositionLocal’s value does not change • If value changes entire screen will be recomposed • compositionLocalOf • If value changes, recompose components that read value
data val LocalColor = staticCompositionLocalOf<Colors> { Error(“No Colors”) } LOCAL SCOPED DATA(COMPOSITIONLOCAL) - JETPACK COMPOSE / / Provide value to “LocalColor” (CompositionLocal) CompositionLocalProvider( LocalColor provides appColors() ) { / / The components within this block can use LocalColors }
TCA is “hot trend” for developers using Compose, SwiftUI • Is it really “trendy” way? • Managing the UI State in a single store(or N store) has already been demonstrated in React’s Flux pattern(Redux)
external source) management + SWR strategy • Actually screen state is closely related to server state(loading, success, etc) • Async State Management explains “we should manage also this state too!” • Example - Optimistic Update • Assume that the server response is successful and maintain the UI state accordingly, but roll it back if the request fails SYNC MORE EASILY WITH SERVER STATE - ASYNC STATE MANAGEMENT
async state management library • There are many features • Caching • Updating stale data when it stales (using staleTime) • Invalidate cache forcefully • Provide loading state, error state to draw screen more easily using those function Example() { const {data, isLoading, isError, refetch} = useQuery(‘index’, fetchData, { staleTime: 5000 } ); if (isLoading) return <Text>Loading… < / Text> if (isError) return <View><ErrorIamge / > < / View> const invalidate = () = > { queryClient.invalidateQueries(‘index’) } return <View>{data}<View onClick={invalidate} >Invalidate < / View> < / View> }
tied to any speci fi c library. • If you understand the essential concepts, you can work with other libraries as well and write code with a certain level of pro fi ciency. • If you're considering about how to effectively use declarative UI, I recommend looking into the methodology in React. • React uses many concepts like React Hooks and local state management, which I couldn't introduce(due to time constraints). • These approaches were developed by those who faced similar challenges years ahead of us as Android developers, so they can become best practices for the services you're currently managing.