Last active
March 25, 2019 15:04
-
-
Save sejr/87548aa3138879e2616610bc1d6190d0 to your computer and use it in GitHub Desktop.
Notes Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState} from 'react'; | |
function Notes() { | |
const [newNote, setNewNote] = useState(''); | |
const [notes, setNotes] = useState([ | |
{ | |
text: 'This is your first note!' | |
} | |
]); | |
// Appends a new note to the end of our list. | |
const addNote = note => setNotes([...notes, note]); | |
// Deletes a note with the provided ID. | |
const deleteNote = id => setNotes(notes.splice(id, 1)); | |
return ( | |
<div className="note-list"> | |
<h1>Notes</h1> | |
<input | |
value={newNote} | |
onChange={e => setNewNote(e.target.value)} | |
placeholder="New note" | |
/> | |
<button onClick={() => addNote({ text: newNote })}> | |
Add Note | |
</button> | |
<hr /> | |
{notes.map((note, index) => ( | |
<Note key={index} data={note} onDelete={deleteNote} /> | |
))} | |
</div> | |
); | |
} | |
function Note({ key, data, onDelete }) { | |
const { text } = data; | |
return ( | |
<div className="note"> | |
<p>{text}</p> | |
<button onClick={() => onDelete(key)}>Delete</button> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment