MapleStory Finger Point

๐Ÿ”ต React

[React] ์ปดํฌ๋„ŒํŠธ, props

HYEJU01 2024. 8. 28. 20:26

https://ko.legacy.reactjs.org/docs/components-and-props.html

 

Components์™€ Props – React

A JavaScript library for building user interfaces

ko.legacy.reactjs.org

 

 

1. ์ปดํฌ๋„ŒํŠธ

๐Ÿ˜บ ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž :) 

 

๐Ÿ˜บ ๋ฆฌ์•กํŠธ์˜ ์ปดํฌ๋„ŒํŠธ๋Š” Tree ๊ตฌ์กฐ๋กœ

1. ํ•จ์ˆ˜ํ˜•

2. ํด๋ž˜์Šคํ˜•

2๊ฐ€์ง€๋ฅผ ๊ฐ€์ง„๋‹ค.

 

 

๐Ÿ“ ํ•จ์ˆ˜ํ˜•

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

 

๐Ÿ“  ํด๋ž˜์Šคํ˜•

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

 

 

๐Ÿ“ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• 

๋ชจ๋“ˆ๋‚ด๋ณด๋‚ด๊ธฐ - ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๋ชจ๋“ˆ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“ˆ๋ถˆ๋Ÿฌ์˜ค๊ธฐ - ๋ถ€๋ชจ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ

 

 

 

 

๐Ÿ˜บ ์ปดํฌ๋„ŒํŠธ ํ•ฉ์„ฑ

  • ์˜ˆ๋ฅผ ๋“ค์–ด Welcome์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ Œ๋”๋งํ•˜๋Š” App ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
//์ƒ์œ„
function App() {
  
return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}

//ํ•˜์œ„
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

 

 

 

2. props (์ค‘์š”)

 

๐Ÿ˜บ props ๊ฐœ๋… 

  • ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž…๋‹ˆ๋‹ค.
  • ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ props๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” {}๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ๊ฐ’์„ ๋™์ผ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
//์ƒ์œ„
const App = () => {
    return (
    	<Fragment>
        	<MyComponent name={"ํ™๊ธธ๋™"} age={2} email={"aa@naver.com"}/>
    	</Fragment>
    )
}

//ํ•˜์œ„
const MyComponent = ( {name, age, addr, email} ) => {
   return (
        <div>
            ๋‚˜์˜ ์ฒซ๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ<br/>
            ํ”„๋กœํผํ‹ฐ1:{name}<br/>
            ํ”„๋กœํผํ‹ฐ2:{age}<br/>
            ๊ธฐ๋ณธ๊ฐ’ ํ”„๋กœํผํ‹ฐ3:{addr}<br/>
        </div>
   )

 

 

๐Ÿ“   props์˜ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ (์ฃผ์˜ - ๋Œ€์†Œ๋ฌธ์ž ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•จ  )

์ปดํฌ๋„ŒํŠธ๋ช….defaultProps = {}

//MyComponent.defaultProps = {
//    addr: '์„œ์šธ์‹œ',
//    age: 0
//}

function MyComponent( {name = "์ด๋ฆ„์—†์Œ" , age = 0, addr = "๊ฒฝ๊ธฐ๋„" }  /* props */  ) { //props๊ฐ’์„ ๋ฐ›์Œ

}

 

 

 

๐Ÿ“   props์˜ ํƒ€์ž…๊ฒ€์ฆ (์ฃผ์˜ - ๋Œ€์†Œ๋ฌธ์ž ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•จ  )

์ปดํฌ๋„ŒํŠธ๋ช….propTypes = {}

import PropTypes from 'prop-types'; //ํ”„๋กญ์Šค ํƒ€์ž…๊ฒ€์‚ฌ
//....

MyComponent.propTypes = {
    name: PropTypes.string,
    age: PropTypes.number,
    email: PropTypes.string.isRequired//ํ•„์ˆ˜๊ฐ’(์ƒ์œ„์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ˜๋“œ์‹œ ์ „๋‹ฌ)
}

 

 

 


 

 



[ App02.js ]

//์˜ˆ์ œ app
import MyComponent from "./component/Mycomponent";

function App(){
  return(
    <div>
      <MyComponent name={"ํ™๊ธธ๋™"} age={20} addr={"์„œ์šธ์‹œ"}/>
      <MyComponent/>
    </div>
  )
}


export default App;
  • Props : ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ 

 

 

 

[ MyComponent.js ]

 

import Proptypes from 'prop-types';

function MyComponent( {name, age, addr} /* props */ ){
  /* function MyComponent( {name="์ด๋ฆ„์—†์Œ", age = 0, addr = "๊ฒฝ๊ธฐ๋„"}  */
  //console.log(props);

  /* name = "์ด์ˆœ์‹ "; ์˜ณ์ง€ ์•Š์€ ๋ฐฉ๋ฒ• */

  return (
    <div>
      3 : {name} {/* props.name */}
      1 : {age}
      2 : {addr}
    </div>
  )
}


MyComponent.defaultProps = {
  name : "์ด๋ฆ„ ์—†์Œ",
  age : "0",
  addr : "์ฃผ์†Œ ์—†์Œ",
}


MyComponent.prototype = {
  name : Proptypes.string,
  age : Proptypes.number,
  addr : Proptypes.string
}

export default MyComponent;

 

 


 

 

  • props ๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ
  return (
    <div>
      3 : {name} {/* props.name */}
      1 : {age}
      2 : {addr}
    </div>
  )

 

 

  • props ๊ธฐ๋ณธ ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ
MyComponent.defaultProps = {
  name : "์ด๋ฆ„ ์—†์Œ",
  age : "0",
  addr : "์ฃผ์†Œ ์—†์Œ",
}

 

 

  • props ํƒ€์ž… ๊ฒ€์ฆ
MyComponent.prototype = {
  name : Proptypes.string,
  age : Proptypes.number,
  addr : Proptypes.string
}