객체 분해 할당으로 props 가져오기
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
const books = [
{
author: 'Charles Dickens',
title: 'Interesting Topics',
img: './images/book-1.jpg',
id: 1,
},
{
author: 'Laura Dave',
title: 'The Last Thing He Told Me',
img: './images/book-2.jpg',
id: 2,
},
];
const BookList = () => {
return (
<section className='booklist'>
{books.map((book) => {
return <Book book={book} key={book.id} />;
})}
</section>
);
};
const Book = ({ book: { img, title, author, children } }) => {
return (
<article>
{/* Dynamically render title */}
<img src={img} alt={title} />
<h2>{title}</h2>
<h4>{author}</h4>
{children}
</article>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<BookList />);
spread oprator로 props 가져오기
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
const books = [
{
author: 'Charles Dickens',
title: 'Interesting Topics',
img: './images/book-1.jpg',
id: 1,
},
{
author: 'Laura Dave',
title: 'The Last Thing He Told Me',
img: './images/book-2.jpg',
id: 2,
},
];
const BookList = () => {
return (
<section className='booklist'>
{books.map((book) => {
return <Book {...book} key={book.id} />;
})}
</section>
);
};
const Book = (props) => {
const { img, title, author, children } = props;
return (
<article>
{/* Dynamically render title */}
<img src={img} alt={title} />
<h2>{title}</h2>
<h4>{author}</h4>
{children}
</article>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<BookList />);
'Research > React' 카테고리의 다른 글
React_prop으로 받아온 함수 작동 안되는 문제. 래퍼 함수, 익명 함수로 해결하기 (0) | 2023.04.24 |
---|---|
React_Form submission (0) | 2023.04.24 |
React_Childeren props 사용하기 (0) | 2023.04.22 |
React_deconstructing으로 props 생략하기 (0) | 2023.04.22 |
React_props 간단히 사용하기 (0) | 2023.04.21 |
댓글