<@UHAJKUSTU> whats the difference between doing an...
# decompose
v
@Arkadii Ivanov whats the difference between doing anything inside the
init {}
block of a component and inside
doOnCreate {}
Copy code
init {
    println("In Init")

    doOnCreate {
        println("in doOnCreate")
    }
}
m
I would say that
init {}
block is called before component lifecycle is created or started, so you have to be mindful of what you do in init. For example if your component holds a
ChildStack
, it would be unsafe to mutate the stack right during
init {}
call because in order to perform navigation, the Component must be at least in created (or started? can’t really tell) state. Otherwise, nothing would happen.
a
The main difference is that onCreate is typically called after the component creation, not during. And you can also control it in tests if needed. That's it.
v
I see So if i want to call apis to setup some listeners Where should i call them? In init or in doOnCreate
a
Both options are valid. I used to use init {} for API/DB calls. But some devs believe it's a code smell.
v
You said i can control
doOnCreate
, does that mean in tests i can change it, to not call apis when component is created?
a
In tests, you can pass LifecycleRegistry and drive it as needed for your tests.
🙂 1
v
ahh, makes sense doOnCreate then seems a better place to make such function calls
a
Well, even if you call your API in onInit, you can still test it by controlling the response. E.g. 1. Create the component 2. Verify loading state 3. Fake the API response 4. Verify data state This is something I've been doing so far.
v
ah, i see Thanks