// 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'>
<EventExamples />
{books.map((book) => {
return <Book {...book} key={book.id} />;
})}
</section>
);
};
const EventExamples = () => {
const handleFormInput = (e) => {
console.log(e.target);
console.log(e.target.name);
console.log(e.target.value);
console.log('handle form input!!');
};
const handleButtonClick = () => {
alert('handle button clicked!! :D');
};
const handleFormSubmission = (e) => {
// avoid re-rendering
e.preventDefault();
console.log('You subbable submission');
};
return (
<section>
<form onSubmit={handleFormSubmission}>
<h2>Typical Form</h2>
<input
type='text'
name='example'
onChange={handleFormInput}
style={{ margin: '1rem' }}
/>
</form>
<button onClick={handleButtonClick}>Click Me</button>
</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_ES6 모듈로 파일 분리하여 코드 클린하게 하기 (0) | 2023.04.24 |
---|---|
React_prop으로 받아온 함수 작동 안되는 문제. 래퍼 함수, 익명 함수로 해결하기 (0) | 2023.04.24 |
React_props 깔끔하게 가져오기 (0) | 2023.04.24 |
React_Childeren props 사용하기 (0) | 2023.04.22 |
React_deconstructing으로 props 생략하기 (0) | 2023.04.22 |
댓글