Adding a Modal for Editing Notes to CloudNotebook App โ๏ธ๐๏ธ
Right now, our CloudNotebook App lets users add and delete notes, but there's no way
to edit a note once it's created. Deleting and re-adding a note just to fix a typo is a bad
user experience. The standard solution used in almost every real-world note-taking app โ including
Google Keep and Evernote โ is to open an edit modal: a popup form pre-filled with the note's
existing data that updates the note in place.
In this tutorial, we will learn:
Notes componentnote state to hold the note currently being editedupdateNote function to open the modal with pre-filled valuesref and a hidden trigger buttoneditNoteWhat is a Modal, and Why Use It for Editing Notes?
A modal is a dialog box that appears on top of the current page, dimming the background and capturing the user's focus until they close it. For editing notes in CloudNotebook, a modal is ideal because it lets users update a note's title, description, and tag without navigating away from their list of notes โ keeping the whole note-editing experience fast and distraction-free.
---Step 1: Add the Bootstrap Modal Markup
Open your Notes.js component and add a Bootstrap 5 modal below your notes list. We'll
also add a hidden button with ref={modalRef} that we'll "click" programmatically
whenever a user wants to edit a note.
<button
type="button"
ref={modalRef}
className="btn btn-primary d-none"
data-bs-toggle="modal"
data-bs-target="#editModal"
>
Launch Edit Modal
</button>
<div className="modal fade" id="editModal" tabIndex="-1" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title">Edit Note</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal"></button>
</div>
<div className="modal-body">
{/* Edit form goes here */}
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
<button type="button" className="btn btn-primary" data-bs-dismiss="modal">
Update Note
</button>
</div>
</div>
</div>
</div>
Step 2: Add note and modalRef to Component State
We need a piece of state to hold the note that is currently being edited, and a ref
so we can open the modal from our JavaScript code instead of a user directly clicking a
visible Bootstrap trigger.
import React, { useState, useRef } from "react";
const Notes = (props) => {
const modalRef = useRef(null);
const [note, setNote] = useState({
id: "",
etitle: "",
edescription: "",
etag: "",
});
return (
<div className="container my-4">
{/* Notes list and modal will go here */}
</div>
);
};
export default Notes;
Step 3: Write the updateNote Function
This function runs when a user clicks the Edit icon on a note. It fills our note
state with that note's current values, then clicks the hidden button to open the modal.
const updateNote = (currentNote) => {
modalRef.current.click();
setNote({
id: currentNote._id,
etitle: currentNote.title,
edescription: currentNote.description,
etag: currentNote.tag,
});
};
Step 4: Build the Edit Form Inside the Modal Body
Now let's fill in the modal body with input fields bound to our note state, so the
form is pre-filled with the note's existing title, description, and tag.
<div className="modal-body">
<form className="my-3">
<div className="mb-3">
<label htmlFor="etitle" className="form-label">Title</label>
<input
type="text"
className="form-control"
id="etitle"
name="etitle"
value={note.etitle}
onChange={onChange}
minLength={5}
required
/>
</div>
<div className="mb-3">
<label htmlFor="edescription" className="form-label">Description</label>
<input
type="text"
className="form-control"
id="edescription"
name="edescription"
value={note.edescription}
onChange={onChange}
minLength={5}
required
/>
</div>
<div className="mb-3">
<label htmlFor="etag" className="form-label">Tag</label>
<input
type="text"
className="form-control"
id="etag"
name="etag"
value={note.etag}
onChange={onChange}
/>
</div>
</form>
</div>
And the matching onChange handler that keeps our note state in sync as the user types:
const onChange = (e) => {
setNote({ ...note, [e.target.name]: e.target.value });
};
Step 5: Save Changes with handleClick
When the user clicks Update Note, we call editNote โ a prop passed down from
our App component (or context) that updates the note by its id โ and pass it the
edited values.
const handleClick = () => {
props.editNote(note.id, note.etitle, note.edescription, note.etag);
};
Wire this up to the Update Note button in the modal footer:
<button
type="button"
disabled={note.etitle.length < 5 || note.edescription.length < 5}
onClick={handleClick}
className="btn btn-primary"
data-bs-dismiss="modal"
>
Update Note
</button>
Step 6: Full Updated Notes.js
Here is the complete Notes.js component with the edit modal fully wired up.
import React, { useState, useRef } from "react";
import NoteItem from "./NoteItem";
const Notes = (props) => {
const modalRef = useRef(null);
const { notes, editNote } = props;
const [note, setNote] = useState({
id: "",
etitle: "",
edescription: "",
etag: "",
});
const updateNote = (currentNote) => {
modalRef.current.click();
setNote({
id: currentNote._id,
etitle: currentNote.title,
edescription: currentNote.description,
etag: currentNote.tag,
});
};
const onChange = (e) => {
setNote({ ...note, [e.target.name]: e.target.value });
};
const handleClick = () => {
editNote(note.id, note.etitle, note.edescription, note.etag);
};
return (
<div className="container my-4">
<h2 className="text-center mb-4">Your Notes ๐๏ธ</h2>
<button
type="button"
ref={modalRef}
className="btn btn-primary d-none"
data-bs-toggle="modal"
data-bs-target="#editModal"
>
Launch Edit Modal
</button>
<div className="modal fade" id="editModal" tabIndex="-1" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title">Edit Note</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal"></button>
</div>
<div className="modal-body">
<form className="my-3">
<div className="mb-3">
<label htmlFor="etitle" className="form-label">Title</label>
<input
type="text"
className="form-control"
id="etitle"
name="etitle"
value={note.etitle}
onChange={onChange}
minLength={5}
required
/>
</div>
<div className="mb-3">
<label htmlFor="edescription" className="form-label">Description</label>
<input
type="text"
className="form-control"
id="edescription"
name="edescription"
value={note.edescription}
onChange={onChange}
minLength={5}
required
/>
</div>
<div className="mb-3">
<label htmlFor="etag" className="form-label">Tag</label>
<input
type="text"
className="form-control"
id="etag"
name="etag"
value={note.etag}
onChange={onChange}
/>
</div>
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
<button
type="button"
disabled={note.etitle.length < 5 || note.edescription.length < 5}
onClick={handleClick}
className="btn btn-primary"
data-bs-dismiss="modal"
>
Update Note
</button>
</div>
</div>
</div>
</div>
<div className="row my-3">
{notes.map((noteItem) => (
<NoteItem
key={noteItem._id}
updateNote={updateNote}
note={noteItem}
/>
))}
</div>
</div>
);
};
export default Notes;
How the Edit Modal Flow Works
| Piece | Purpose | Triggered When |
|---|---|---|
modalRef |
Programmatically opens the hidden Bootstrap modal trigger button | Inside updateNote, when the user clicks a note's edit icon |
note state |
Holds the id, title, description, and tag of the note being edited | Pre-filled by updateNote, updated live by onChange |
editNote (prop) |
Updates the actual note in the notes list/backend by its id | Called inside handleClick on "Update Note" click |
Features and Learnings:-
Notes component.ref to open the modal from JavaScript, not a visible click.onChange handler to keep form inputs in sync with state.