React

React STEP 17 - JSX에서 배열 사용

2025. 2. 10. 15:47
728x90

⭐ JSX에서의 배열

 

1. 단계 1

class Round extends React.Component {
  render() {
    let roundStyle = {
      display: "inline-block",
      margin:10,
      width: 100,
      height: 100,
      backgroundColor: this.props.backgroundColor,
      borderRadius: 50
    }

    return(
      <div style={roundStyle}></div>
    )
  };
  
}

root.render (
  <Round></Round>
)

 

2. 단계 2

class Round2 extends React.Component {
  render() {
    let roundStyle = {
      display: "inline-block",
      margin:10,
      width: 100,
      height: 100,
      backgroundColor: this.props.backgroundColor,
      borderRadius: 50
    }

    return (
      <div style={roundStyle}></div>
    )
  }
}

let round1 = <Round2 backgroundColor="#f90"></Round2>;
let round2 = <Round2 backgroundColor="#eaeaea"></Round2>;

root.render (
  <div>
    {round1}
    {round2}
  </div>
)
  • 해석
let round1=<Round backgroundColor="#f90" />;
let round2=<Round backgroundColor="#eaeaea" />;
클래스 컴포넌트를 인스턴스 화하여 사용되고 있는 것을 알 수 있습니다.
인스턴스는 복제물이라고 생각하면 되고, 클래스는 복제물의 원본이 되는 설계도라고 생각하면 
됩니다

 

3. 단계 3

class Round3 extends React.Component {
  render() {
    let roundStyle={
      display: "inline-block",
      margin: 10,
      width: 100,
      height: 100,
      backgroundColor: this.props.backgroundColor,
      borderRadius: 50
    };
    return(
        <div style={roundStyle}></div>
    ) 
  }
}

let drawNewRound=() => {
  let colors=["#f90", "#09f", "#eaeaea", "#ccc"];
  let randomColor=Math.floor(Math.random()*colors.length);
  return <Round3 backgroundColor={colors[randomColor]} />
};

root.render (
  <div>
    {drawNewRound()}
    {drawNewRound()}
    {drawNewRound()}
    {drawNewRound()}
  </div>
)

 

4. 단계 4

class Round4 extends React.Component {
  render(){
      let roundStyle={
          display: "inline-block",
          margin: 10,
          width: 100,
          height: 100,
          backgroundColor: this.props.backgroundColor,
          borderRadius: 50
      };
      return(
          <div style={roundStyle}></div>
      );
  }
}

let colors=["#f90", "#09f", "#8ac007", "#eaeaea", "#ccc", "#999"];

let componentData=colors.map((d, i) => <Round4 key={i+1} backgroundColor={d} />);

root.render (
  <div>
    {componentData}
  </div>
)

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'React' 카테고리의 다른 글

React STEP 19 - Create React App - 1  (0) 2025.02.10
React STEP 18 - React 이벤트  (0) 2025.02.10
React STEP 16 - State - 2  (0) 2025.02.10
React STEP 15 - State - 1  (1) 2025.02.10
React STEP 14 - Props의 사용 - 4  (0) 2025.02.10
'React' 카테고리의 다른 글
  • React STEP 19 - Create React App - 1
  • React STEP 18 - React 이벤트
  • React STEP 16 - State - 2
  • React STEP 15 - State - 1
IT의 큰손
IT의 큰손
IT계의 큰손이 되고 싶은 개린이의 Log 일지
IT의 큰손
Developer Story House
IT의 큰손
전체
오늘
어제
  • 분류 전체보기 (457)
    • 정보처리기사 필기 (18)
    • 정보처리기사 실기 (12)
    • 정보처리기사 통합 QUIZ (12)
    • 빅데이터 (11)
    • 안드로이드 (11)
    • 웹페이지 (108)
    • 자바 (49)
    • SQLD (3)
    • 백준 알고리즘 (76)
    • 데이터베이스 (41)
    • 깃허브 (2)
    • Library (14)
    • Server (31)
    • 크롤링&스크래핑 (3)
    • Spring (23)
    • Vue.js (13)
    • React (27)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Developer Stroy House

인기 글

태그

  • 데이터베이스
  • 웹개발
  • 백엔드
  • 정보처리기사필기
  • 코딩테스트
  • html
  • 앱개발자
  • java
  • css
  • 프론트엔드
  • 개발자
  • 웹개발자
  • 개발블로그
  • React
  • it
  • 웹페이지
  • 백준
  • jsp
  • IT자격증공부
  • jquery
  • 정보처리기사
  • JavaScript
  • 알고리즘
  • DB
  • IT자격증
  • 정보보안전문가
  • DBA
  • IT개발자
  • 자바
  • ajax

최근 댓글

최근 글

Designed By hELLO
IT의 큰손
React STEP 17 - JSX에서 배열 사용
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.