티스토리 뷰

import { createElement } from "react";

function Container({ children }: { children: JSX.Element | JSX.Element[] }) {
  const childArray = Array.isArray(children) ? children : [children];

  return (
    <>
      {childArray.map((child, idx) => {
        return createElement(child.type, {
          ...{
            ...child.props,
            idx: idx,
            value: `value${idx}`
          }
        });
      })}
    </>
  );
}

 

예시 : <Container>의 자식으로 여러개의 노드를 사용하는 경우

JSX.Element[] 배열을 통해 선언되어 있는 모든 자식 노드를 가져오고, 

child를 콘솔에 찍어보면 타입, props값 등 해당 노드의 정보를 가져오거나 확인할 수 있다. 

 

예를들어, 

child.props.name이라고 하면 자식노드의 name props 값을 가져와 사용할 수 있다. 

 

 

'SW프로그래밍 > React' 카테고리의 다른 글

[React] 리덕스 프로젝트  (0) 2021.06.02
[React] redux 라이브러리  (0) 2021.05.13
[React] api 호출, Axios  (1) 2021.05.12
[react List] react-virtualized 스크롤 핸들링  (0) 2021.04.29
[React]SPA , React-Router  (0) 2021.04.27
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함