State

How do I add state to my render component?

The render method provides state as a parameter. However, in order to allow to a component to perform a state update, the component has to provide an initial state, even if it's nil.

Initial State

// Provide any initial state
+ (id)initialState;

Initial State from Props

In case your initial state relies on the component props, you can use the following method instead:

// Provide an initial state from the component's props
+ (id)initialStateWithComponent:(id<CKRenderComponentProtocol>)component;

Example:

@implementation MyRenderComponent
{
NSString *_text;
}
+ (instancetype)newWithText:(NSString *)text
{
auto const c = [super new];
if (c) {
c->_text = text;
}
return c;
}
- (id)initialState
{
return @NO;
}
- (CKComponent *)render:(id)state
{
const BOOL multiLine = [state boolValue];
return multiLine
? [MultiLineTextComponent newWithText:text]
: [SingleLineTextComponent newWithText:text];
}
@end