Frontity Tutorial
  • 👋Welcome
  • ✔️Checking your progress
  • 1️⃣ Creating a custom theme
    • Create a Frontity Project
    • Create a Theme Package
    • Modify the <Root> component
    • Connect the <Root> component to the state
  • 2️⃣ Adding a menu
    • Use the <Link> component
  • 3️⃣ Displaying posts
    • Understanding the Frontity state
    • Display the list of posts
    • Display the post content
    • Display posts and pages separately
    • Formatting the date
  • 4️⃣ Adding styling
    • Global styles
    • Styled components
    • Styling the links
    • Styling the post info
    • Add dynamic styling
  • 5️⃣ Adding behaviour
    • Use 'state' and 'actions'
    • Add pagination
  • 6️⃣ Custom Post Types
    • Add support for Custom Post Types
  • 7️⃣ Finishing touches
    • Add a <Loading> component
    • Add a 404 page
    • Use the <html2react> component
    • SEO and head tags
    • Other things to do
  • 8️⃣ Deploying the project
    • Deploy to Vercel
  • 9️⃣ Next Steps
    • Learning resources
    • Getting help and support
    • Contributing to Frontity
Powered by GitBook
On this page
  1. 3️⃣ Displaying posts

Display the post content

At the moment we have a list of clickable post links, but when we click them we simply see the text "This is a post". However you will notice that the URL is changing. This then is a good place to display the content of each post.

Let's create a new file called post.js inside the components directory. This will contain the <Post> component which we will use to display the title and the content of the posts.

Note the use of the two step data retrieval process in this code. We first use state.source.get, the helper function, to get the relevant item from state.source.data, and then we use the data.type property to determine where in the state we should fetch the item from, i.e. whether it's a post or a page.

// File: /packages/my-first-theme/src/components/post.js

import React from "react"
import { connect } from "frontity"

const Post = ({ state }) => {
  const data = state.source.get(state.router.link)
  const post = state.source[data.type][data.id]

  return (
    <div>
      <h2>{post.title.rendered}</h2>
      <div dangerouslySetInnerHTML={{ __html: post.content.rendered }} />
    </div>
  )
}

export default connect(Post)

As before, we need to import it into the root component file and use it to display posts and pages.

// File: /packages/my-first-theme/src/components/index.js

// ...
import Post from "./post"

const Root = ({ state }) => {
  const data = state.source.get(state.router.link)

  return (
    <>
      {/* ... */}
      <Switch>
        <List when={data.isArchive} />
        <Post when={data.isPost} />
        <Post when={data.isPage} />
      </Switch>
    </>
  )
}

Note that for now we are using the <Post> component whether data.isPost or data.isPage is true - so this component will be used whether you click on a post link or on the 'About Us' link in the menu.

Now we can see the post title and the content. 🙌

PreviousDisplay the list of postsNextDisplay posts and pages separately

Last updated 4 years ago

Check you're on the right track by comparing your changes with .

the ones in this commit