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

  1. willMount
  2. componentDidAcquireView if the controller's component has a view
  3. didMount 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.

  1. willRemount
  2. If the controller's component creates a view and its previous view cannot be recycled:
    1. componentWillRelinquishView
    2. componentDidAcquireView
  3. didRemount

Unmounting

  1. willUnmount
  2. componentWillRelinquishView if the controller's component has a view
  3. didUnmount

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:

  1. willUpdateComponent before willMount or willRemount
  2. didUpdateComponent after didMount or didRemount

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 in componentWillRelinquishView.
  • 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 in componentWillRelinquishView.

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.