React

React STEP 14 - Props의 사용 - 4

2025. 2. 10. 13:43
728x90

⭐ Props

  • 컴포넌트 속성 전달하기
  • Component1 -> Component2 -> Component3 내용 전달 

1. 단계 1

class Component3 extends React.Component {
  render() {
    return (
      <div>
        <p>{this.props.color}</p>
        <p>{this.props.name}</p>
        <p>{this.props.size}</p>
      </div>
    )
  }
}

class Component2 extends React.Component {
  render() {
    return (
      <Component3 color={this.props.color} name={this.props.name} size={this.props.size}></Component3>
    )
  }
}

class Component1 extends React.Component {
  render() {
    return (
      <Component2 color={this.props.color} name={this.props.name} size={this.props.size}></Component2>
    )
  }
}

root.render(
  <Component1 color="#f90" name="orange" size="small"></Component1>
)

 

2. 단계 2

  • 전개 연산자(spread operator, ...) 을 사용하면 이러한 전달 코드를 조금 더 간단하게 작성할 수 있습니다.
const obj1={shape: "triangle", x: 42};
const obj2={shape: "rectangle", y: 12};
const cloneObj={...obj1};
// console.log(cloneObj); // {shape: "triangle", x: 42}
const mergeObj={...obj1, ...obj2};
// console.log(mergeObj); // {shape: "rectangle", x: 42, y: 12}
  • 예시
class Component6 extends React.Component {
  render() {
    return (
      <div>
        <p style={{ color: this.props.color }}>{this.props.color}</p>
        <p>{this.props.name}</p>
        <p>{this.props.size}</p>
      </div>
    )
  }
}

class Component5 extends React.Component {
  render() {
    return (
      <Component6 {...this.props}></Component6>
    )
  }
}

class Component4 extends React.Component {
  render(){
      return(
          <Component5 {...this.props} />
      );  
  }
}

root.render(
  <Component4 color="#f90" size="small" name="orange"></Component4>
)
728x90
저작자표시 비영리 변경금지 (새창열림)

'React' 카테고리의 다른 글

React STEP 16 - State - 2  (0) 2025.02.10
React STEP 15 - State - 1  (1) 2025.02.10
React STEP 13 - Props의 사용 - 3  (0) 2025.02.10
React STEP 12 - Props의 사용 - 2  (0) 2025.02.10
React STEP 11 - props의 사용 - 1  (2) 2025.02.07
'React' 카테고리의 다른 글
  • React STEP 16 - State - 2
  • React STEP 15 - State - 1
  • React STEP 13 - Props의 사용 - 3
  • React STEP 12 - Props의 사용 - 2
IT의 큰손
IT의 큰손
IT계의 큰손이 되고 싶은 개린이의 Log 일지
Developer Story HouseIT계의 큰손이 되고 싶은 개린이의 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

인기 글

태그

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

최근 댓글

최근 글

Designed By hELLO
IT의 큰손
React STEP 14 - Props의 사용 - 4
상단으로

티스토리툴바

단축키

내 블로그

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

블로그 게시글

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

모든 영역

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

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