๐Ÿ“–[React]Component ์ž‘์„ฑ

ํ˜ฑยท2022๋…„ 7์›” 7์ผ

React

๋ชฉ๋ก ๋ณด๊ธฐ
6/28

๐Ÿ’— Component์˜ ์ข…๋ฅ˜

โœ Function Component

์ฝ”๋“œ๊ฐ€ ๊ฐ„๋‹จํ•œ ๊ฒƒ์ด ํŠน์ง•, Component๋ฅผ ํ•จ์ˆ˜ ์ทจ๊ธ‰

function Welcome(props){
	return <h1>์•ˆ๋…•, {props.name} </h1>;
   };

โœ Class Component

React.Component๋ฅผ ์ƒ์†๋ฐ›์•„์„œ Component๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ

Class Welcome extends React.Component {
  render() {
    return <h1> ์•ˆ๋…•, {this.props.name}</h1>;
  }
}

๐Ÿ’— Component์˜ ์ด๋ฆ„

ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค!
์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉด domc ํƒœ๊ทธ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ

const element = <div/> 

๐Ÿ’— Component ํ•ฉ์„ฑ๊ณผ ํ˜ธ์ถœ

๋ณต์žกํ•œ ํ™”๋ฉด์„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋ˆ„์–ด์„œ ํ™”๋ฉด ๊ตฌํ˜„ ๊ฐ€๋Šฅ

โœ Component ํ•ฉ์„ฑ

ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํฌํ•จํ•˜์—ฌ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ

โœ Component ์ถ”์ถœ

ํฐ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•˜์—ฌ, ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ž„

๐Ÿ’— ์ฝ”๋“œ ์˜ˆ์‹œ

//Comment(ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ)

const styles= {
  wrapper: {
    margin:8,
    padding:8,
    display:"flex",
    flexDirection: "row",
    border: "1px solid gray",
    borderRadius: 16
  },
  imageContainer:{},
  image: {
    width: 50,
    height: 50,
    borderRadius: 25,
  },
  contentContainer:{
    marginLeft:8,
    display: "flex",
    flexDirection: "column",
    justifyContent: "center",
  },
  nameText: {
    color: "black",
    fontSize: 16,
    fontWeight: "bold",
  },
  commentText: {
    color: "black",
    fontSize: 16,
  },
};

function Comment(props) {
  return(
    <div style={styles.wrapper}>
      <div style = {styles.imageContainer}>
        <img
          src= "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png"
          style={styles.image}
          />
      </div>
      
      <div style={styles.contentContainer}>
        <span style={styles.nameText}>{props.name}</span>
        <span style={styles.commentText}>
          {props.comment}
        </span>
      </div>
      
      
      </div>
    );
}

export default Comment;
//์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ
import Comment from "./Comment";

const comments = [
  {
    name: "์ด๋ฏผํ˜",
    comment:"ํ•˜์ดํ•˜์ด",
  },
  {
    name: "๋ˆ„๊ตฌ๊ฒŒ",
    comment: "๋ฆฌ์•กํŠธ ํใ…œ",
  },
  {
    name: "ํž˜๋“œ๋Ÿฌ",
    comment:"ํ›„ํ›„ํ›„ ์–ธ์ œ..",
  },
];

function CommentList(props){
  return( 
    <div> 
      {comments.map((comment) => {
        return (
          <Comment name={comment.name} comment={comment.comment}/>
          );
      })}
      </div>
    );
}

export defualt CommentList;
profile
new blog: https://hae0-02ni.tistory.com/

0๊ฐœ์˜ ๋Œ“๊ธ€