๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React

[ React ] React Element ์™€ Component์˜ ์ฐจ์ด์ 

 

React Element

React ์—˜๋ฆฌ๋จผํŠธ๋Š” React ์•ฑ์˜ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋กœ, ๋ธŒ๋ผ์šฐ์ € DOM ์—˜๋ฆฌ๋จผํŠธ์™€ ๋‹ค๋ฅด๊ฒŒ ์ผ๋ฐ˜ ๊ฐ์ฒด(plain object)์ด๋ฉฐ, ์‰ฝ๊ฒŒ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

React ์—˜๋ฆฌ๋จผํŠธ๋Š” ํ™”๋ฉด์— ํ‘œ์‹œํ•  ๋‚ด์šฉ์„ ๊ธฐ์ˆ ํ•˜๋ฉฐ, React DOM์€ React ์—˜๋ฆฌ๋จผํŠธ์™€ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.

 

const element = <h1>Hello, world</h1>;

 

React ์—˜๋ฆฌ๋จผํŠธ๋Š” ๋ถˆ๋ณ€๊ฐ์ฒด์ด๋ฉฐ, ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•œ ์ดํ›„์—๋Š” ํ•ด๋‹น ์—˜๋ฆฌ๋จผํŠธ์˜ ์ž์‹์ด๋‚˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ์—˜๋ฆฌ๋จผํŠธ๋Š” ์˜ํ™”์—์„œ ํ•˜๋‚˜์˜ ํ”„๋ ˆ์ž„๊ณผ ๊ฐ™์ด ํŠน์ • ์‹œ์ ์˜ UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

 

 

์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ๋„ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. (Welcome ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—˜๋ฆฌ๋จผํŠธ๋กœ ์‚ฌ์šฉ)

const element = <Welcome name="Sara" />;

 

React๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด, JSX ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์™€ ์ž์‹์„ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์— ๋‹จ์ผ ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•˜๋Š”๋ฐ, ์ด ๊ฐ์ฒด๋ฅผ "props"๋ผ๊ณ  ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

React Component 

react ์ปดํฌ๋„ŒํŠธ๋Š” ์ž‘๊ณ  ๊ณ ๋ฆฝ๋œ ์ฝ”๋“œ์˜ ํŒŒํŽธ์œผ๋กœ, ์ด๋Ÿฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ UI๋ฅผ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐœ๋ณ„์ ์ธ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„๊ณ , ๊ฐ ์กฐ๊ฐ๋“ค์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋Š” ๊ฐœ๋…์ ์œผ๋กœ๋Š” JavaScript์˜ ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•˜๋‹ค. "props"๋ผ๋Š” ์ž„์˜์˜ ์ž…๋ ฅ์„ ๋ฐ›์€ ๋’ค, ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋ ์ง€ ๊ธฐ์ˆ ํ•˜๋Š” React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ UI๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉฐ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

const ShoppingList = () => {
	return(
    	<>
            <div className="shopping-list">
                <h1>Shopping List for {this.props.name}</h1>
                <ul>
                  <li>Instagram</li>
                  <li>WhatsApp</li>
                  <li>Oculus</li>
                </ul>
            </div>
       </>
    )
}

export default ShoppingList;

 

 

 

 

 

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

JavaScript ํ•จ์ˆ˜๋กœ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

 

Welcome ํ•จ์ˆ˜๋Š” props(์†์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ์ดํ„ฐ) ๊ฐ์ฒด ์ธ์ž๋ฅผ ๋ฐ›์€ ํ›„ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

 

 

 

 

 

 

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ

ES6์˜ class๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 

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

 

 

 

 

 

 


 

๐ŸŒธ ์ถœ์ฒ˜ ๐ŸŒธ

 

Rendering Elements - React

Components and Props -React

React.Component - React