Hello!
I am having trouble figuring out how to get a prop sent to a component. Simply I want the component to list posts that are filtered by my criteria.
For example
<AuthorRoll name={title} />
Would take in the Author’s name and filter posts that are for that Author only.
Here is my AuthorRoll.js page
import React from "react"
import PropTypes from "prop-types"
// Components
import { Link, graphql, StaticQuery } from "gatsby"
import PreviewCompatibleImage from '../preview-compatible-image'
import './rolls.scss'
class AuthorRoll extends React.Component {
render() {
const { data } = this.props
const { edges: posts, totalCount: tcount } = data.allMarkdownRemark
const { author } = "test"
const tagHeader = `${tcount} post${
tcount === 1 ? "" : "s"
} tagged with "${author}"`
return (
<div>
<h1 className="roll-header">{tagHeader}</h1>
{posts &&
posts.map(({ node: post }) => (
<article
className={`roll-post-container ${
post.frontmatter.featuredpost ? 'is-featured' : ''
}`}
>
<div className="roll-post-content">
<span className="roll-category">Category</span>
<Link
className="roll-title"
to={post.fields.slug}
>
{post.frontmatter.title}
</Link>
<Link
className="roll-details roll-excerpt"
to={post.fields.slug}
>
{post.excerpt}
</Link>
<div>
<div className="roll-author">
{post.frontmatter.author}
</div>
<div className="roll-details">
<span>{post.frontmatter.date} · </span>
<span>{post.timeToRead} min read</span>
</div>
</div>
</div>
<div className="roll-image-container">
{post.frontmatter.featuredimage ? (
<Link
className="roll-post-image"
to={post.fields.slug}
>
<PreviewCompatibleImage
imageInfo={{
image: post.frontmatter.featuredimage,
alt: `featured image thumbnail for post ${post.frontmatter.title}`,
}}
/>
</Link>
) : null}
</div>
</article>
))}
</div>
)
}
}
AuthorRoll.propTypes = {
data: PropTypes.shape({
allMarkdownRemark: PropTypes.shape({
totalCount: PropTypes.number.isRequired,
edges: PropTypes.arrayOf(
PropTypes.shape({
node: PropTypes.shape({
frontmatter: PropTypes.shape({
title: PropTypes.string.isRequired,
author: PropTypes.string.isRequired,
}),
fields: PropTypes.shape({
slug: PropTypes.string.isRequired,
}),
}),
}).isRequired
),
}),
}),
}
export default () => (
<StaticQuery
query={graphql`
query($author: String) {
allMarkdownRemark(
limit: 2000
sort: { fields: [frontmatter___date], order: DESC }
filter: {frontmatter: {templateKey: {eq: "blog-post"}, author: {in: "[$author]"}}}
) {
totalCount
edges {
node {
excerpt(pruneLength: 400)
fields {
slug
}
timeToRead
frontmatter {
title
templateKey
author
date(formatString: "MMMM DD, YYYY")
featuredimage {
childImageSharp {
fluid(maxWidth: 120, quality: 100) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
}
}
`}
render={(data, count) => <AuthorRoll data={data} count={count} />}
/>
)
Is it possible to reference a prop from the parent and use it in this child component? If I manually add the filter to the author name I get the posts I want but it would be nice to reference the author name from the parent page.