Search

์ƒํ’ˆ ๊ด€๋ฆฌ FrontEnd (React)

์ƒํ’ˆ ๊ด€๋ฆฌ ์‚ฌ์ดํŠธ๋ฅผ ๊ตฌํ˜„

React ๋กœ ์ƒํ’ˆ ๋ชฉ๋ก ํŽ˜์ด์ง€ ๊ตฌํ˜„

Card.jsx

import React, { useState } from 'react' import { Favorite, FavoriteBorder } from "@mui/icons-material"; // const Card = (props) => { // const Card = ({title, content}) => { const Card = ({ card }) => { const { no, title, content, likes, img } = card // โญ state ์„ ์–ธ const [like, setLike] = useState(false) const [count, setCount] = useState(likes) // โญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ const handleLike = () => { setLike( !like ) setCount( !like ? count+1 : count-1 ) console.log(`like : ${like}`) } return ( <div style={{ border: "1px solid #ddd", borderRadius: "10px", overflow: "hidden", boxShadow: "0 5px 10px rgba(0,0,0,0.2)", margin: "0 20px" }}> {/* ์ƒํ’ˆ ์ด๋ฏธ์ง€ */} <img src={ img } width={300} height={200} style={{ objectFit: 'contain' }} alt="์ƒํ’ˆ์ด๋ฏธ์ง€" /> {/* ์ปจํ…์ธ  */} <div style={{ padding: "10px" }}> <h3 style={{ fontSize: "20px", fontWeight: "bold" }}> {/* {props.title} */} { title } </h3> <p style={{ color: "#666", fontSize: "14px" }}> {/* {props.content} */} { content } </p> <button style={{ border: "none", backgroundColor: "transparent", display: "flex", justifyContent: "center", alignItems: "center", }} onClick={ handleLike } > { like ? <Favorite style={{ color: "#ff4757", fontSize: "40px" }} /> : <FavoriteBorder style={{ color: "#ff4757", fontSize: "40px" }} /> } <span style={{ marginLeft: "5px", fontSize: "24px" }}> { count } </span> </button> </div> </div> ) } export default Card
JavaScript
๋ณต์‚ฌ

CardList.jsx

import React, { useEffect, useState } from 'react' import Card from './Card' const CardList = () => { // Card ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•  ๋ฐ์ดํ„ฐ const cardData = [ { no : 1, title: '์•„๋ฉ”๋ฆฌ์นด๋…ธ', content: '์ฝœ๋กฌ๋น„์•„ ์›๋‘๋กœ ๋งŒ๋“ ...', likes: 100, img: 'https://i.imgur.com/D1ic2Xk.jpg' }, { no : 2, title: '์นดํŽ˜๋ผ๋–ผ', content: '๋ผ๋–ผ๋Š” ๋ง์ด์•ผ...', likes: 200, img: 'https://i.imgur.com/hAzIEVv.jpg' }, { no : 3, title: '์ฟ ํ‚คํ”„๋ผํŽ˜', content: '์˜ค๋ ˆ์˜ค ๋‹คํ–‡๋„ค...', likes: 300, img: 'https://i.imgur.com/kDhIhLv.jpg' }, ] // โ” useEffect() ํ›… ์‚ฌ์šฉ // "๋ Œ๋”๋ง ์‹œ ํ˜ธ์ถœ ๋˜๋Š” ํ•จ์ˆ˜" // : ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ๋งˆ์šดํŠธ, ์—…๋ฐ์ดํŠธ, ์–ธ๋งˆ์šดํŠธ ์ƒ๋ช…์ฃผ๊ธฐ ์ž‘์—…์„ // ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํ›… useEffect(() => { getProductList(); }, []); return ( <div> <h1>์ƒํ’ˆ ๋ชฉ๋ก</h1> <div style={{ display: "grid", gridTemplateColumns: "repeat(4, auto)", rowGap: "40px" }}> { cardData.map( (card, index) => { // return <Card key={card.no} title={card.title} content={card.content} /> // return <Card key={card.no} {...card} /> return <Card key={card.no} card={ card } /> }) } </div> </div> ) } export default CardList
JavaScript
๋ณต์‚ฌ