我如何过滤段塞数据



我正在使用sanity来获取分段数据,我只是不想显示来自同一帖子类别的相关帖子。

我试图获取所有数据并将其传递给状态。但我不知道如何获得当前的帖子类别和过滤数据。我想我需要使用useRouter,但我不确定。谢谢你的帮助!

(弹头).tsx


/// MY ATTEMPT
const [data, setData] = useState([] as any [])
useEffect(() => {
sanityClient.fetch(`*[ _type == "postschema"] {
_id,
title,
category,
}`
).then(result => {
setData(result)
})
}, [])
///
const SingleProject = ({projects} : any) => {
return (
<>
<>
<div className="single__page">
<div className="down__arrow">

</div>
<div className="single__header">
<h2>{projects.category}</h2>
{*HERE I WANT TO SHOW SIMILAR POSTS FROM THE SAME CATEGORY*}
</div>
</div>
}
export const getServerSideProps: GetServerSideProps = async ({params}) => {
const query = `*[_type == "projectschema" && slug.current == $id][0] {
_id,
title,
category,
date,
website,
thumbnail,
client,
company,
description,
review,
author,
authorjob,
image1,
image2,
image3,
image4,
image5,
slug
}`;

const projects = await sanityClient.fetch(query, {
id: params?.id,

})
if(!projects) {
return {
notFound: true
}
}

return {
props: {
projects,
}
}
}

像这样?

{
data.filter(a=>a.category === projects.category)
.map(r => {
return (
<div>
output info {project.category}
</div>
)
})
}

最新更新