Learn How to Add an Edit Notes Modal to CloudNotebook App ๐โ๏ธ
Right now, CloudNotebook App lets users add and delete notes โ but there's no way to
update a note once it's created. In this tutorial, we'll add an Edit Notes Modal:
a popup form that opens with the note's existing title and description pre-filled, so users
can make changes and save them instantly, without leaving the page.
In this tutorial, we will learn:
updateNote function in Context to save changesWhat is an Edit Notes Modal?
A modal is a popup window that appears on top of the current page without navigating away from it. For a notes app like CloudNotebook, an edit modal is the most user-friendly way to update a note โ the user clicks Edit, a form pops up already filled with the note's current title and description, they make their changes, hit Update Note, and the modal closes with the note updated instantly on the page.
---Step 1: Add an updateNote Function to NotesContext
Open NotesContext.js and add an updateNote function. It finds the note by
id and replaces its title and description with the new values.
const updateNote = (id, title, description) => {
setNotes(
notes.map((note) =>
note._id === id ? { ...note, title, description } : note
)
);
};
Don't forget to expose it through the provider's value:
<NoteContext.Provider value={{ notes, addNote, deleteNote, updateNote }}>
{props.children}
</NoteContext.Provider>
Step 2: Add State to Track the Note Being Edited
In Notes.js, add state to hold the note that's currently open in the Edit Modal.
We'll pre-fill this whenever the user clicks Edit on a note card.
const [note, setNote] = useState({
id: "",
etitle: "",
edescription: "",
});
Step 3: Write the updateNote Click Handler
This function opens the modal with the selected note's data already filled in.
const updateNote = (currentNote) => {
setNote({
id: currentNote._id,
etitle: currentNote.title,
edescription: currentNote.description,
});
};
Step 4: Build the Edit Notes Modal
Using Bootstrap's Modal classes, let's add the Edit Notes Modal markup inside Notes.js.
It stays hidden until triggered, and contains a small form with two fields: title and description.
<div
className="modal fade"
id="editNoteModal"
tabIndex="-1"
aria-labelledby="editNoteModalLabel"
aria-hidden="true"
>
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="editNoteModalLabel">
Edit Note
</h5>
<button
type="button"
className="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></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>
<textarea
className="form-control"
id="edescription"
name="edescription"
value={note.edescription}
onChange={onChange}
minLength={5}
required
/>
</div>
</form>
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button
disabled={note.etitle.length < 5 || note.edescription.length < 5}
type="button"
className="btn btn-warning"
onClick={handleUpdateClick}
>
Update Note
</button>
</div>
</div>
</div>
</div>
Step 5: Handle Input Changes and Submit the Update
Add an onChange handler for the modal's form fields, and a handleUpdateClick
function that calls updateNote from context and closes the modal programmatically.
const onChange = (e) => {
setNote({ ...note, [e.target.name]: e.target.value });
};
const handleUpdateClick = () => {
context.updateNote(note.id, note.etitle, note.edescription);
// Close the modal using Bootstrap's JS API
document.getElementById("closeEditModalBtn").click();
};
Add a hidden close button with the id referenced above, right inside the modal's
footer, so we can trigger a close without the user manually clicking Close:
<button
type="button"
id="closeEditModalBtn"
data-bs-dismiss="modal"
style={{ display: "none" }}
></button>
Step 6: Add an Edit Button to Every Note Card
Finally, inside NoteItem.js, add an Edit icon/button that opens the modal
and loads the clicked note's data using data-bs-toggle and data-bs-target.
<i
className="fa-solid fa-pen-to-square mx-2"
data-bs-toggle="modal"
data-bs-target="#editNoteModal"
onClick={() => updateNote(note)}
></i>
Step 7: Full Updated Notes.js
Here is the combined Notes.js with state, the modal, and all handlers wired together.
import React, { useContext, useState } from "react";
import NoteItem from "./NoteItem";
import noteContext from "../context/notes/noteContext";
const Notes = () => {
const context = useContext(noteContext);
const { notes } = context;
const [note, setNote] = useState({
id: "",
etitle: "",
edescription: "",
});
const updateNote = (currentNote) => {
setNote({
id: currentNote._id,
etitle: currentNote.title,
edescription: currentNote.description,
});
};
const onChange = (e) => {
setNote({ ...note, [e.target.name]: e.target.value });
};
const handleUpdateClick = () => {
context.updateNote(note.id, note.etitle, note.edescription);
document.getElementById("closeEditModalBtn").click();
};
return (
<>
{/* Edit Note Modal */}
<div
className="modal fade"
id="editNoteModal"
tabIndex="-1"
aria-labelledby="editNoteModalLabel"
aria-hidden="true"
>
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="editNoteModalLabel">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>
<textarea
className="form-control"
id="edescription"
name="edescription"
value={note.edescription}
onChange={onChange}
minLength={5}
required
/>
</div>
</form>
</div>
<div className="modal-footer">
<button type="button" id="closeEditModalBtn" data-bs-dismiss="modal" style={{ display: "none" }}></button>
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button
disabled={note.etitle.length < 5 || note.edescription.length < 5}
type="button"
className="btn btn-warning"
onClick={handleUpdateClick}
>
Update Note
</button>
</div>
</div>
</div>
</div>
{/* Notes List */}
<div className="row my-4">
{notes.map((n) => (
<NoteItem key={n._id} note={n} updateNote={updateNote} />
))}
</div>
</>
);
};
export default Notes;
How the Edit Notes Modal Works
| Piece | Purpose | Triggered By |
|---|---|---|
note state |
Holds the id, title, and description of the note currently being edited | Clicking the Edit icon on a note card |
data-bs-toggle / data-bs-target |
Opens the Bootstrap modal without extra JavaScript | Edit icon click |
updateNote() (Context) |
Updates the matching note in the global notes array by _id |
Clicking "Update Note" in the modal |
closeEditModalBtn |
Programmatically closes the modal right after a successful update | Clicking "Update Note" in the modal |
Features and Learnings:-
updateNote function to Context to update a note by its id.