Display a Note

Now that we have a listing of all the notes, let's create a page that displays a note and lets the user edit it.

The first thing we are going to need to do is load the note when our container loads. Just like what we did in the Home container. So let's get started.

Add the Route

Let's add a route for the note page that we are going to create.

Add the following line to src/Routes.tsx below our /notes/new route. We are using the AppliedRoute component that we created in the [Add the session to the state]({% link _chapters/add-the-session-to-the-state.md %}) chapter.

<AppliedRoute path="/notes/:id" exact component={Notes} appProps={appProps} />

This is important because we are going to be pattern matching to extract our note id from the URL.

By using the route path /notes/:id we are telling the router to send all matching routes to our component Notes. This will also end up matching the route /notes/new with an id of new. To ensure that doesn't happen, we put our /notes/new route before the pattern matching one.

And include our component in the header.

import Notes from "./containers/Notes";

Of course this component doesn't exist yet and we are going to create it now.

Add the Container

Create a new file src/containers/Notes.tsx and add the following.

import React, { useRef, useState, useEffect } from "react";
import { API, Storage } from "aws-amplify";
import { RouteComponentProps } from "react-router-dom";
type RouteParams = {
id: string; // must be type string since route params
};
interface NotesProps extends RouteComponentProps<RouteParams> {}
const Notes: React.FC<NotesProps> = (props: NotesProps) => {
const file = useRef(null);
const [note, setNote] = useState(null);
const [content, setContent] = useState("");
useEffect(() => {
const loadNote = () => {
return API.get("notes", `/notes/${props.match.params.id}`, {});
};
const onLoad = async () => {
try {
const note = await loadNote();
const { content, attachment } = note;
if (attachment) {
note.attachmentURL = await Storage.vault.get(attachment);
}
setContent(content);
setNote(note);
} catch (e) {
alert(e);
}
};
onLoad();
}, [props.match.params.id]);
return <div className="Notes"></div>;
};
export default Notes;

We are doing a couple of things here.

  1. We are using the useEffect Hook to load the note when our component first loads. We then save it to the state. We get the id of our note from the URL using the props automatically passed to us by React-Router in props.match.params.id. The keyword id is a part of the pattern matching in our route (/notes/:id).

  2. If there is an attachment, we use the key to get a secure link to the file we uploaded to S3. We then store this in the new note object as note.attachmentURL.

  3. The reason why we have the note object in the state along with the content and the attachmentURL is because we will be using this later when the user edits the note.

Now if you switch over to your browser and navigate to a note that we previously created, you'll notice that the page renders an empty container.

Empty notes page loaded screenshot

Next up, we are going to render the note we just loaded.