3 ways to edit Markdown with TinaCMS + Gatsby
weibenfalk
Posted on January 9, 2020
Originally published on TinaCMS.org
Supercharge your static site with real-time content editing! đ
In this post, I will explore the three different methods Tina offers to edit Markdown on your Gatsby site. Youâll learn how to set up Tina with both Page Queries and Static Queries.
This post will not cover the basics of using Tina with Gatsby. Please reference the documentation on how to initially set up Tina with Gatsby.
Whatâs the deal with Page Queries and Static Queries?
Before we dive down into editing Markdown with Tina we have to understand how Gatsby handles querying data with GraphQL. You can source data from almost anywhere in Gatsby. In our case, weâre using Markdown. When you build your site, Gatsby creates a GraphQL schema for all the data. Then you use GraphQL in your React components to query your sourced data.
Gatsby allows you to query your data in two ways: Page Queries and Static Queries.
Since the release of the React Hooks API and the useStaticQuery
hook in Gatsby, it is very easy to query your data. There are cases when you canât use a Static Query though. First, letâs explore the differences.
The two main differences are:
- Page Queries can accept GraphQL variables. Static Queries canât.
- Page Queries can only be added to page components. Static Queries can be used in all components.
So, why canât we use GraphQL variables in a Static Query? The reason for that is a Static Query doesnât have access to the page context like a Page Query does. The result is that a Static Query wonât be able to access variables that are defined in the page context. You can define the page context in your gatsby-node.js
file in your createPage
function. Here you can supply your page with different variables that will get injected to your page on build time.
I use Static Queries as much as possible because I love the hooks API and the ease of composition possibilities it brings. For example, you can create custom hooks and reuse them in multiple components.
Letâs say that you have a GraphQL query that grabs metadata that you want on multiple pages. Create a custom React hook with the useStaticQuery
Gatsby hook inside of it. Then you can use your custom hook wherever you want and always easily get that data into your component. When you need to have variables in your component, you have to use a Page Query. Page Queries cannot be used with the hooks API and have to be unique and attached to the specific page component.
Another great thing with Static Queries is that you can grab the data in the component that needs the data. That prevents prop drilling and your data is more tightly coupled to the component where it is used.
React overview
For getting data, we can use Gatsby's query options. For structuring our components, React also offers a couple of options. You can either create your component as a class or a functional component. Before the React Hooks API, you had to use class components to have state in your components. Now with hooks, you can do this with functional components.đ„°
Three ways to edit markdown with Tina
Given all the options for creating components and souring data in Gatsby, we have to choose the most suitable approach for the project. Tina can work with all of these options, providing three different approaches for editing Markdown with Gatsby as described below.
- remarkForm - A Higher Order Component used when you source data from a Page Query in Gatsby. This component can be utilized with both functional and class components. Please note the subtle difference here! The only difference in naming from the render props component is the lowercase ârâ.
-
useLocalRemarkForm - A React Hook that is intended for functional components sourcing data from either a Static or a Page Query. If the component is sourcing static data, Gatsby's
useStaticQuery
hook would be called. -
RemarkForm - A Render Props Component that you can use in class components sourcing data from either a Static or a Page Query. Static data would be sourced via Gatsbyâs
StaticQuery
render props component.
remarkForm - How to use it and why it wonât work with Static Queries.
First, Letâs dive into how to hook up TinaCMS with a Page Query.
The remarkForm
Component in TinaCMS is a Higher Order Component, a HOC in short. This means that it is a function that takes in another component and will return a new component that has added functionality to it.
If youâre not familiar with HOC's, I suggest you read about them in the React official docs. They are considered âadvanced usageâ in the React world.
The remarkForm
component wants another component as an argument and is intended for Page Queries. A Page Query injects the data as a prop to the component and we access the data from this prop. With a useStaticQuery
hook, the data is collected in a variable, that you choose, inside the component itself. That means if you're using the useStaticQuery
hook in Gatsby you wonât have a component to give the remarkForm
HOC. Bummer!đ Thatâs why you can only use the remarkForm
component with Page Queries.
So how do you use this component with a Page Query in Gatsby? First, check out the fictive Star Wars component below. It will show the three steps needed to hook everything up:
// 1. Import the `remarkForm` HOC
import { remarkForm } from 'gatsby-tinacms-remark'
const StarWarsMovie = ({ data: { markdownRemark } }) => {
return <h1>{markdownRemark.frontmatter.title}</h1>
}
// 2. Wrap your component with `remarkForm`
export default remarkForm(StarWarsMovie)
// 3. Add the required ...TinaRemark fragment to your Page Query
export const pageQuery = graphql`
query StarWarsMovieById($id: String!) {
markdownRemark(fields: { id: { eq: $id } }) {
id
html
frontmatter {
title
releaseDate
crawl
}
...TinaRemark
}
}
`
The above code is a component that displays information about Star Wars movies. For now, it just displays a title, but it could also display the release date and the crawl text in the intro to the film. But thatâs another story in a galaxy far far away ... â
The first step in this example is to import the remarkForm
hook from the Gatsby plugin âgatsby-tinacms-remarkâ. This is the plugin that makes TinaCMS work with Markdown files.
Thereâs no need to do any additions to the code inside of the component itself. It could be any component â functional or class â structured in the way you want it. The only thing you have to do with the component itself is to wrap your component with the remarkForm
HOC when you export it.
One more thing you have to do before you are good to go is to add the GraphQL fragment ...TinaRemark
in your query. This is needed for TinaCMS to recognize your data and create the required editor fields in the TinaCMS sidebar. After that, you only have to start up your dev server to show the site and the Tina sidebar.
Easy enough isnât it? Just three small steps and youâll have a beautiful sidebar to edit your content on your site. đ€
But what if you want to use a Static Query and not a Page Query?
useLocalRemarkForm to the rescue!
Weâve learned that the remarkForm
HOC wonât work on Static Queries. So weâll have to find another solution for using Static Queries with TinaCMS.
Great news!
The remarkForm
component is essentially a "wrapper" for the useLocalRemarkForm
hook. đ It takes in a component as an argument, calls useLocalRemarkForm
with the Page Query data and returns a new component with the query data and TinaCMS connected to it.
We can use the useLocalRemarkForm
hook directly, without using the remarkForm
HOC. This can be useful with Static Queries or if we just prefer working with hooks!
Take a look at the code example below to get an idea of how useLocalRemarkForm
works.
// 1. Import useLocalRemarkForm hook
import React from âreactâ;
import { useLocalRemarkForm } from âgatsby-tinacms-remarkâ;
import { useStaticQuery } from âgatsbyâ;
const StarWarsMovie = () => {
// 2. Add required TinaCMS fragment to the GrahpQL query
const data = useStaticQuery(graphql`
query StarWarsMovieById {
markdownRemark(fields: { id: { eq: "sw-01" } }) {
id
html
frontmatter {
title
releaseDate
crawl
}
...TinaRemark
}
}
`);
// 3. Call the useLocalRemarkForm hook and pass in the data
const [markdownRemark] = useLocalRemarkForm(data.markdownRemark);
return <h1>{markdownRemark.frontmatter.title}</h1>
}
export default StarWarsMovie;
This is just an example component illustrating how useLocalRemarkForm
works. In the real world, it would not be an optimal solution using a Static Query for this. Thatâs because, as you can see, you canât use variables inside the useStaticQuery
hook to make it dynamic. You have to hardcode the movie id. So this query will work for that specific movie only, which is no good.
Letâs break down whatâs happening here:
- We import the
useLocalRemarkForm
custom hook so we can use it in our component. - Just as before, the
...TinaRemark
fragment is needed in the GraphQL query. So we add that one there. - When weâve got our data back from the Gatsby
useStaticQuery
hook we can call the TinaCMSuseLocalRemarkForm
hook with that data. This hook will return an array with two elements. The first element is practically the data that we called the hook with. It has the same shape and is ready for us to use in our component. The second element is a reference to the Tina form. We donât need that one so we donât destructure it out as we do with themarkdownRemark
.
If you're wondering about this line:
const [markdownRemark] = useLocalRemarkForm(heroData.markdownRemark)
It is an example of ES6 destructuring. As we get an array with two elements back, I destructure out the first element (which is our data) and name it markdownRemark
. You can name it whatever you want.
RemarkForm - The Render Prop Component
You canât use React Hooks on class components. Thatâs why Tina provides a RemarkForm
component that uses the Render Props pattern.
This component works with both Page and Static Queries. I will show how to use it with a Page Query below.
Take a look at below example:
// 1. import the RemarkForm render prop component
import { RemarkForm } from '@tinacms/gatsby-tinacms-remark'
class StarWarsMovie extends React.Component {
render() {
/*
** 2. Return RemarkForm, pass in markdownRemark
** to the remark prop and pass in what you
** want to render to the render prop
*/
return (
<RemarkForm
remark={this.props.data.markdownRemark}
render={({ markdownRemark }) => {
return <h1>{markdownRemark.frontmatter.title}</h1>
}}
/>
)
}
}
export default StarWarsMovie
// 3. Add the required ...TinaRemark fragment to your Page Query
export const pageQuery = graphql`
query StarWarsMovieById($id: String!) {
markdownRemark(fields: { id: { eq: $id } }) {
id
html
frontmatter {
title
releaseDate
crawl
}
...TinaRemark
}
}
`
Ok, yet again, letâs see whatâs happening here:
- We import the
RemarkForm
component for us to use in our code. - In our return statement we return the
RemarkForm
component and pass in it's predefined, and required props. The remark prop providesRemarkForm
with the markdown data sourced from the Page Query. The render prop gets the JSX that we want to render through a function, or a render prop.RemarkForm
will hook up Tina for editing the data and then render whatever is specified in the render prop function. - Just as before we have to add the
...TinaRemark
fragment to the Page Query.
Next steps
That's it! Three ways of using Tina for editing Markdown files in Gatsby. đ
In this post, we learned about how to set up Tina with both Static Queries and Page Queries in Gatsby. We also learned about three different ways to edit markdown with Tina depending on your type of React component.
This is just the basics to get you started. If you like Tina and want to learn more you should check out the official docs. Thereâs a lot more stuff to read there and some interesting use cases.
For example, you can learn how to apply inline editing and also how to customize the form fields in the Tina sidebar.
Tina is a great addition to the React ecosystem and static site generators like Gatsby. It gives your site a pleasant and easy way to edit and interact with your content.
Iâm thrilled to see how big TinaCMS will be and what it can do as it evolves!
More reading and learning
Tina on Twitter: @tina_cms
Watch my tutorial for Tina & Gatsby. Also catch me on Twitter â @weibenfalk, Youtube, or on my website.
Posted on January 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.