React 의 Props

강주현·2021년 11월 21일
0

Studies - Dream coding

목록 보기
6/8
post-thumbnail

Props

▶ 부모로부터 상속받은 속성( property ) 으로, 컴포넌트 밖에서 주어지는 데이터이다.

  • 컴포넌트 외부에서 데이터를 전달받는것들로, HTML 로 예를 들자면,
    내가 사용하려는 컴포넌트의 외부에서부터 전달받은 Props 들이 attrubute 와, attribute의 값들로 적용된다고 생각하면 된다.

        
    // App.JSX
    class App extends Component {
      
      // title : 멤버변수
      title = "강주현";
            
      // handle : 멤버함수
      handle = () => {
        console.log("버튼을 눌렀다.");
      };
    
            
      render() {
        return <LikeButton title={this.title} onClick={this.handle} />;
      }
    }
        
        
        
    // LikeButton.JSX
    class LikeButton extends Component {
    
      state = {
        numberOfLikes: 'flower',
      };
    
      render() {
          console.log(this.props); // { title: '강주현', onCilck : f} 
          console.log(this.state); // { numberOfLikes : 'flower'}
         return <button className={this.props.title}>{this.state.numberOfLikes}</button>;
          // <button class="강주현">flower</button>
      }
    }
    /* 해석 :  	
    	LikeButton 컴포넌트는, 
    	LikeButton 의 부모 컴포넌트인, App 컴포넌트로부터 (App 컴포넌트에 있는)
    	title 이라는 멤버변수와 handle 이라는 멤버변수를 인수로 전달받았다.*/
    }
profile
프론트엔드 개발자가 되기 위해 열심히 달리고 있는 꼬꼬마개발자

0개의 댓글