【发布时间】:2021-07-02 17:47:16
【问题描述】:
我被卡住了,迷路了...我正在使用 gatsby-plugin-algolia,当我搜索时,会过滤空字段。它们由 PostPreview 组件生成。
我在模板文件夹中的 archive.js(列出博客文章):
imports....
// algolia search
import algoliasearch from 'algoliasearch/lite'
import { InstantSearch, SearchBox, Hits } from 'react-instantsearch-dom'
import PostPreview from '../components/postPreview/PostPreview'
const searchClient = algoliasearch(
'MY KEY...',
'MY KEY...'
)
const archiveTemplate = ({
data: { allWpPost },
pageContext: { catId, catName, catURI, categories, numPages, currentPage }
}) => {
return (
<Layout>
<Wrapper>
<BreadCrumbs
parent={{
uri: '/blog/todos-os-posts',
title: 'Blog'
}}
/>
<ContentWrapper>
<ArchiveSidebar catId={catId} categories={categories.edges} />
<PageContent>
<InstantSearch searchClient={searchClient} indexName="Gatsby">
<SearchBox />
<Hits hitComponent={PostPreview} />
</InstantSearch>
<h1 dangerouslySetInnerHTML={{ __html: catName }} />
{allWpPost.edges.map(post => (
<article key={post.node.id} className="entry-content">
<StyledDate
dangerouslySetInnerHTML={{ __html: post.node.date }}
/>
<Link to={`/blog${post.node.uri}`}>
<StyledH2
dangerouslySetInnerHTML={{ __html: post.node.title }}
/>
</Link>
<p dangerouslySetInnerHTML={{ __html: post.node.excerpt }}></p>
<StyledReadMore to={`/blog${post.node.uri}`}>
Leia +
</StyledReadMore>
<div className="dot-divider" />
</article>
))}
<Pagination
catUri={catURI} // from the context
page={currentPage}
totalPages={numPages}
/>
</PageContent>
</ContentWrapper>
</Wrapper>
</Layout>
)
}
export default archiveTemplate
export const pageQuery = graphql`
query($catId: String!, $skip: Int!, $limit: Int!) {
allWpPost(
filter: { categories: { nodes: { elemMatch: { id: { eq: $catId } } } } }
skip: $skip
limit: $limit
) {
edges {
node {
id
title
excerpt
uri
slug
date(formatString: "DD, MMMM, YYYY", locale: "pt")
}
}
}
}
`
我在 Gatsby-config.js 中的 Algolia:
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`
})
const AlgoliaBloghQuery = `
{
allWpPost {
nodes {
title
excerpt
id
}
}
}
`
作品:
我的 PostPreview 组件,我迷路了。
imports...
const Postpreview = ({ id, title, date, excerpt, uri }) => {
return (
<div>
<article key={id} className="entry-content">
<StyledDate dangerouslySetInnerHTML={{ __html: date }} />
<Link to={`/blog${uri}`}>
<StyledH2 dangerouslySetInnerHTML={{ __html: title }} />
</Link>
<p dangerouslySetInnerHTML={{ __html: excerpt }}></p>
<StyledReadMore to={`/blog${uri}`}>Leia +</StyledReadMore>
<div className="dot-divider" />
</article>
</div>
)
}
export default Postpreview
我不知道如何让数据传递给道具,我不知道查询是否冲突(archive.js - gatsby-config)。仅呈现阅读更多按钮。
【问题讨论】:
标签: reactjs graphql gatsby algolia