React Router

Next we need to create the router in App.js. Our app will have two main routes:

  1. A home route - /. This route will render a list of posts from our API
  2. A post details route - /post/:id. This route will render a single post and details about that post.

Using React Router, we can read the Post ID from the route, and then fetch the post associated with it. This is a common pattern in many apps as it makes the link shareable.

Another way to do this would be to have some global state management set up and then set the post ID in the global state. The main drawback of this approach is that the URL cannot be shared.

Other than routing, the main functionality happening in this component is a call to fetch posts from our GraphQL API.

Paste the code below into the src/App.js file:


import React, { useState, useEffect } from "react";
import {
} from "react-router-dom";
import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
import { css } from 'emotion';
import { API, Storage, Auth } from 'aws-amplify';
import { listPosts } from './graphql/queries';

import Posts from './Posts';
import Post from './Post';
import Header from './Header';
import CreatePost from './CreatePost';
import Button from './Button';

function Router() {

  /* create a couple of pieces of initial state */
  const [showOverlay, updateOverlayVisibility] = useState(false);
  const [posts, updatePosts] = useState([]);

  /* fetch posts when component loads */
  useEffect(() => {
  }, []);

  async function fetchPosts() {

    /* query the API, ask for 100 items */
    let postData = await API.graphql({ query: listPosts, variables: { limit: 100 }});
    let postsArray =;

    /* map over the image keys in the posts array, get signed image URLs for each image */
    postsArray = await Promise.all( post => {
      const imageKey = await Storage.get(post.image);
      post.image = imageKey;
      return post;

    /* update the posts array in the local state */
  async function setPostState(postsArray) {

  return (
          <div className={contentStyle}>
            <Header />
            <hr className={dividerStyle} />
            <Button title="New Post" onClick={() => updateOverlayVisibility(true)} />
              <Route exact path="/" >
                <Posts posts={posts} />
              <Route path="/post/:id" >
                <Post />
          <AmplifySignOut />
        { showOverlay && (

const dividerStyle = css`
  margin-top: 15px;

const contentStyle = css`
  min-height: calc(100vh - 45px);
  padding: 0px 40px;

export default withAuthenticator(Router);