Skip to content

Instantly share code, notes, and snippets.

@bigsaigon333
Last active October 7, 2021 08:10
Show Gist options
  • Save bigsaigon333/a33b5d9cdcde6250f783e661229bfef4 to your computer and use it in GitHub Desktop.
Save bigsaigon333/a33b5d9cdcde6250f783e661229bfef4 to your computer and use it in GitHub Desktop.
Array.prototype.{map, reduce, join} 에 착안한 선언적 리액트 컴포넌트 만들기
const arr = [
{ id: 1, text: "abc" },
{ id: 2, text: "def" },
{ id: 3, text: "ghi" },
];
const Map = ({ list, children }) => <>{list.map((el) => children(el))}</>;
const Join = ({ separator, children }) => {
// const [Frag] = React.Children.toArray(children);
// console.log(Frag);
// const L = React.Children.map(Frag, (Comp, i) => {
// console.log(i);
// return Comp;
// });
// console.log(L);
console.log(children);
return <div> {children}</div>;
};
const List = () => (
<ul>
<Join separator={<br />}>
<Map list={arr}>{({ id, text }) => <li key={id}>{text}</li>}</Map>
</Join>
</ul>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment