Scopes
In the following tree, ComponentKit has no way to distinguish the three ListItems:
We want a way to give each child a unique identifier:
Scopes give components a persistent, unique identity. They’re needed in three cases:
- Components that have state must have a scope.
- Components that have a controller must have a scope.
- Components that have child components with state or controllers may need a scope, even if they don’t have state or controllers.
Defining a Scope
Use the CKComponentScope
type to define a component scope at the top of a component’s +new
method.
+ (instancetype)newWithModel:(Model *)model
{
CKComponentScope scope(self, model.uniqueID);
...
return [super newWithComponent:...];
}
If your component doesn’t have a model object with a unique identifier, you can omit that parameter as long as there won’t be multiple siblings of the same type.
CKComponentScope scope(self);