Created
January 27, 2020 10:11
-
-
Save iamandrewluca/35d7e1fa10c379d1c3e306413742d29a 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
import React from "react"; | |
import ReactDOM from "react-dom"; | |
function App() { | |
return ( | |
<div> | |
<hr /> | |
{joinJSX( | |
[ | |
<span key={3}>1</span>, | |
<span key={1}>2</span>, | |
<span key={2}>3</span> | |
], | |
<hr /> | |
)} | |
<hr /> | |
</div> | |
); | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); | |
function joinJSX(components, separator) { | |
const joined = []; | |
for ( | |
let i = 0, l = components.length, c = components[i]; | |
i < l; | |
++i, c = components[i] | |
) { | |
if (joined.length !== 0) { | |
joined.push({ ...separator, key: separator.type + i }, c); | |
} else { | |
joined.push(c); | |
} | |
} | |
return joined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment