Lifecycle Methods
Component controllers expose lifecycle methods that allow you to perform custom operations as components mount, unmount, update, and acquire views.
Whenever possible, avoid using lifecycle methods. Think of them as an emergency escape hatch for integrating with stateful code.
Ordering
Lifecycle methods have the following defined ordering.
Mounting
willMount
componentDidAcquireView
if the controller's component has a viewdidMount
after the component and all of its children are mounted
Remounting
Remounting occurs when the controller's component is already mounted and either the component is being updated or the root component is being attached to a different view.
willRemount
- If the controller's component creates a view and its previous view cannot be recycled:
componentWillRelinquishView
componentDidAcquireView
didRemount
Unmounting
willUnmount
componentWillRelinquishView
if the controller's component has a viewdidUnmount
There are no guarantees that the component's children or parents are mounted in willUnmount
or componentWillRelinquishView
. You must not use CKComponentActionSend
or any other method that assumes the component's parents are mounted.
Updating
The controller's component can be updated to a new version of the component as part of the mounting or remounting process. In this case, you'll receive the following callbacks:
willUpdateComponent
beforewillMount
orwillRemount
didUpdateComponent
afterdidMount
ordidRemount
Uses
Mutating the View
Generally views are a hidden implementation detail of ComponentKit, but you may need to break that abstraction:
- Animations that cannot be implemented using
animationsFromPreviousComponent:
. Be sure you remove all animations incomponentWillRelinquishView
. - Interfacing with views that only expose event callbacks via a delegate API. Make the component controller the view's delegate in
componentDidAcquireView
and nil out the view's delegate incomponentWillRelinquishView
.
In your componentDidAcquireView
and componentWillRelinquishView
implementations you may only reference the view via the component controller's view
property. Remember, these lifecycle methods are called before the component has finished being mounted. Trying to use the component's view context will not work until the component has been mounted.