【发布时间】:2021-12-19 02:46:11
【问题描述】:
这是我的博客页面的代码。
import React, {useState, useEffect} from "react";
import { Link } from "react-router-dom";
import sanityClient from "../client.js"
export default function Post(){
const [postData, setPost] = useState(null);
useEffect(() => {
sanityClient
.fetch(`*[_type == "post"]{
title,
slug,
mainImage{
asset->{
_id,
url
},
alt
}
}
}`)
.then((data) => setPost(data))
.catch(console.error);
}, []);
return(
<main className="bg-blue-100 min-h-screen p-12">
<section className="container mx-auto">
<h1 className="text-5xl flex justify-center cursive">
Updates Page
</h1>
<h2 className="text-lg text-gray-600 flex justify-center mb-12">
School Updates
</h2>
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
{postData && postData.map((post, index) => (
<article>
<Link to={"/post/" + post.slug.current} key={post.slug.current}>
<span className="block h-64 relative rounded shadow leading-snug bg-white border-l-8 border-blue-400" key={index}>
<img src={post.mainImage.asset.url}
alt={post.mainImage.alt}
className="w-full h-full rounded-r object-cover absolute"
/>
<span className="block relative h-full flex justify-end items-end pr-4 pb-4">
<h3 className="text-grey-800 text-lg font-bold px-3 py-4 bg-blue-700 text-blue-100 bg-opacity-75 rounded">
{post.title}
</h3>
</span>
</span>
</Link>
</article>
))}
</div>
</section>
</main>
)
}
它编译得很好,但不会从 sanity 客户端加载到博客文章中。
因为在健全性架构“post.js”中,我在所有内容上都有绿色下划线:“在导出为模块默认 eslint(import/no-anonymous-default-export) 之前将对象分配给变量”
我查看了它指向我的文档,但我只是不明白问题所在。对我来说似乎很好。有人请告诉我哪里出错了。
提前致谢!
【问题讨论】:
-
乍一看,这看起来应该可以工作。我会尝试的几件事是:(1)
console.log(client.config())并确保您的客户端已配置,以及 (2) 将.then((data) => setPost(data))更改为.then((data) => console.log(data) || setPost(data))以确保您收到一系列帖子。 -
@GeoffBall 表示它无法解析整个表达式。 .fetch 中的某些内容是错误的,我只是看不到它。