-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
48 lines (39 loc) · 1006 Bytes
/
Copy pathApp.js
File metadata and controls
48 lines (39 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { useState } from 'react';
import BookCreate from './components/BookCreate';
import BookList from './components/BookList';
function App() {
const [books, setBooks] = useState([]);
const editBookById = (id, newTitle) => {
const updatedBooks = books.map((book) => {
if (book.id === id) {
return { ...book, title: newTitle };
}
return book;
});
setBooks(updatedBooks);
};
const deleteBookById = (id) => {
const updatedBooks = books.filter((book) => {
return book.id !== id;
});
setBooks(updatedBooks);
};
const createBook = (title) => {
const updatedBooks = [
...books,
{
id: Math.round(Math.random() * 9999),
title,
},
];
setBooks(updatedBooks);
};
return (
<div className="app">
<h1>Reading List</h1>
<BookList onEdit={editBookById} books={books} onDelete={deleteBookById} />
<BookCreate onCreate={createBook} />
</div>
);
}
export default App;