【发布时间】:2020-02-16 00:18:56
【问题描述】:
我正在 Gatsby 博客中制作一个可重用组件,放在博客文章的末尾,该组件采用文章的作者姓名,并呈现“关于作者”部分,从存储在其他地方的作者简历中提取信息地点。
我已经制作了当 authorName 被硬编码为字符串时可以工作的组件,但是我被困在如何从文章组件(作为 props.author 传递)中获取作者姓名到 GraphQl 查询中。
我可以确认: 1. prop author 正确传递,控制台日志完全正确 2. 根据硬编码字符串作者姓名从团队简历中提取正确的简历信息和头像
缺少的链接是用props.author替换硬编码的作者姓名字符串
提前感谢您的帮助;我查看了类似的问题,但找不到答案。
这是我的组件:
AboutAuthor.js
import React from "react";
import { StaticQuery, graphql } from "gatsby";
import Img from "gatsby-image";
export default (props) => (
<div>
<StaticQuery
query={graphql`
query {
copy: markdownRemark(
frontmatter: { name: { eq: "need to replace this string with props.author" } }
) {
html
frontmatter {
name
profileImage {
childImageSharp {
fluid(maxWidth: 800) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`}
render={data => (
<React.Fragment>
<p>Testing Name: {props.author}</p> // testing the author name is passed ok, will be removed
<Img
fluid={data.copy.frontmatter.profileImage.childImageSharp.fluid}
alt=""
/>
<div>
<span>
{data.copy.frontmatter.name}
</span>
<div
dangerouslySetInnerHTML={{ __html: data.copy.html }}
/>
</div>
</React.Fragment>
)}
/>
</div>
);
【问题讨论】:
-
添加到 ksav 的答案,我会让这个组件“笨拙”并从其父组件接收
postAuthor数据 -
@DerekNguyen 感谢您的回复,但我不明白。该组件确实从其父组件(post body 组件)获取 postAuthor。在这种情况下,我不希望从帖子中呈现道具,而是使用 postAuthor 来“搜索”作者的简历并呈现该数据。
标签: javascript reactjs graphql gatsby