Created
August 5, 2020 22:40
-
-
Save Jordan-Gilliam/f6e2d7fa150e82a9ad5bf2a89bd9b83d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ________ 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