Skip to content

Instantly share code, notes, and snippets.

@Jordan-Gilliam
Created August 5, 2020 22:40
Show Gist options
  • Save Jordan-Gilliam/f6e2d7fa150e82a9ad5bf2a89bd9b83d to your computer and use it in GitHub Desktop.
Save Jordan-Gilliam/f6e2d7fa150e82a9ad5bf2a89bd9b83d to your computer and use it in GitHub Desktop.
/* ________ RENDER "FACTORY" ________
-> lower level react methods allow more control over child rendering
-> cloneElement is necessary to ensure immutability
-> ugly but allows us to handle use-cases with one component or many
-> cloneElement ensures referential equality & immutability :)
-> ugly but allows us to handle use-cases with one child component or many
*/
let elements = React.Children.toArray(props.children);
if (elements.length === 1) {
elements = React.cloneElement(elements[0], { className: 'top bottom' });
} else if (elements.length > 0) {
let lastElement = elements[elements.length - 1];
elements = [React.cloneElement(elements[0], { className: 'top' })]
.concat(elements.slice(1, -1))
.concat(React.cloneElement(lastElement, { className: 'bottom' }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment