Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about best practice when building multiple children #38

Closed
foulkesjohn opened this issue Mar 27, 2015 · 4 comments
Closed

Question about best practice when building multiple children #38

foulkesjohn opened this issue Mar 27, 2015 · 4 comments
Labels

Comments

@foulkesjohn
Copy link

I have a component which will be used in a collectionview. This component in turn can be built up with many other components so I have a CKStackLayoutComponent which I want to insert multiple children. The number of children will be dynamic (in reality no more than 4), what is the best way to build these up and pass to the children of a CKStackLayoutComponent?

@natansh
Copy link
Contributor

natansh commented Mar 27, 2015

The best way to add a dynamic number of children is to build up a std::vector of CKStackLayoutComponentChild using methods like push_back.

std::vector<CKStackLayoutComponentChild> children = {};
for (ModelType *model in models) {
  // Create component from model, let's say the name of the component you create is childComponent.
  children.push_back(childComponent);
}

And then you can pass in the children directly to CKStackLayoutComponent.

If you already know the exact number beforehand, I would suggest you just create the children inline with CKStackLayoutComponent.

@ocrickard
Copy link
Contributor

If your children are truly dynamic then you can do push_back, but as Natansh hints at: it's rare that's the case.

Check out http://componentkit.org/docs/avoid-push-back.html

@natansh natansh closed this as completed Mar 27, 2015
@adamjernst
Copy link
Contributor

One last tip: FB::map is a great helper. See ComponentUtilities.h.

@foulkesjohn
Copy link
Author

Thanks, this is exactly what I was after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants