fullstackopen-notes/parts/2/renderingCollections/src/App.jsx

50 lines
1.2 KiB
React
Raw Normal View History

import { useState } from "react";
2025-05-20 16:21:42 +02:00
import Note from "./components/Note";
const App = ({ startingNotes }) => {
const [notes, setNotes] = useState(startingNotes);
const [newNote, setNewNote] = useState("a new note...");
const [showAll, setShowAll] = useState(true);
const addNote = (event) => {
event.preventDefault();
const newNoteObject = {
content: newNote,
important: Math.random() < 0.5,
id: String(notes.length + 1),
};
setNotes(notes.concat(newNoteObject));
setNewNote("");
};
const handleNoteChange = (event) => {
console.log(event.target.value);
setNewNote(event.target.value);
};
const notesToShow = showAll
? notes
: notes.filter((note) => note.important === true);
2025-05-20 16:21:42 +02:00
return (
<div>
<h1>Notes</h1>
<div>
{" "}
<button onClick={() => setShowAll(!showAll)}>
{" "}
show {showAll ? "important" : "all"}{" "}
</button>{" "}
</div>
<ul>{notesToShow.map((note) => Note({ note }))}</ul>
<form onSubmit={addNote}>
<input value={newNote} onChange={handleNoteChange} />
<button type="submit">save</button>
</form>
2025-05-20 16:21:42 +02:00
</div>
);
};
export default App;