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. 1️⃣ Creating a custom theme

Connect the <Root> component to the state

PreviousModify the <Root> componentNext2️⃣ Adding a menu

Last updated 4 years ago

Let’s now connect the <Root> component to the Frontity state using connect.

connect is a higher order component that takes a React component as an argument. It passes the Frontity object to the React component specified in the argument via props. The Frontity object has amongst its properties state, actions and libraries. connect therefore enables a component in our theme to access data stored in the state, or functions available in actions.

This also ensures that the component is re-rendered whenever any value from the state which the component is using is changed.

We'll be covering the Frontity state in detail shortly in the section entitled .

In order to connect our component to the Frontity state we need to import { connect } from "frontity", pass state as a (destructured) prop to our component, and finally export the connected component with export default connect(Root).

We can check that our <Root> component is connected to the state by adding a <p> element to our component to display the URL we are currently in. We can do this by using state.router.link.

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

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

const Root = ({ state }) => {
  return (
    <>
      <h1>Frontity Workshop</h1>
      <p>Current URL: {state.router.link}</p>
    </>
  )
}

export default connect(Root)

Now when you save the file we can see that we are in the root of our site: /.

Frontity in the browser

You can try changing the URL in the browser's address bar to something like to see how the value of state.router.link changes.

Frontity in the browser

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

Understanding the Frontity state
http://localhost:3000/about-us
the ones in this commit