List All the Notes
Now that we are able to create a new note, let’s create a page where we can see a list of all the notes a user has created. It makes sense that this would be the homepage (even though we use the /
route for the landing page). So we just need to conditionally render the landing page or the homepage depending on the user session.
Currently, our Home container is very simple. Let’s add the conditional rendering in there.
Replace our src/containers/Home.js
with the following.
import React, { useState, useEffect } from "react";
import { PageHeader, ListGroup } from "react-bootstrap";
import "./Home.css";
export default function Home(props) {
const [notes, setNotes] = useState([]);
const [isLoading, setIsLoading] = useState(true);
function renderNotesList(notes) {
return null;
}
function renderLander() {
return (
<div className="lander">
<h1>Scratch</h1>
<p>A simple note taking app</p>
</div>
);
}
function renderNotes() {
return (
<div className="notes">
<PageHeader>Your Notes</PageHeader>
<ListGroup>
{!isLoading && renderNotesList(notes)}
</ListGroup>
</div>
);
}
return (
<div className="Home">
{props.isAuthenticated ? renderNotes() : renderLander()}
</div>
);
}
We are doing a few things of note here:
-
Rendering the lander or the list of notes based on
props.isAuthenticated
. -
Store our notes in the state. Currently, it’s empty but we’ll be calling our API for it.
-
Once we fetch our list we’ll use the
renderNotesList
method to render the items in the list.
And that’s our basic setup! Head over to the browser and the homepage of our app should render out an empty list.
Next we are going to fill it up with our API.
For help and discussion
Comments on this chapterIf you liked this post, please subscribe to our newsletter, give us a star on GitHub, and follow us on Twitter.