【发布时间】:2022-07-11 23:02:39
【问题描述】:
我正在构建一个 NextJs 应用程序,但由于某种原因,我没有从页面“Feed.tsx”获取函数以在“Index.tsx”中呈现。但是,当我导航到“/feed”时,该功能会完美呈现。这是为什么呢?
Feed.tsx
import Post from "../components/Post";
function PostList({ posts }){
return( <>
{posts?.map((post) => {
return(
<div key={post.id}>
<Post post={post}/>
</div>
)
})}
</>
)
}
export default PostList
export async function getServerSideProps(){
const response = await fetch('http://localhost:3000/posts/')
if (!response.ok) {
const message = `An error occured: ${response.statusText}`;
window.alert(message);
return;
}
const data = await response.json();
console.log(data);
return{
props:{
posts: data,
},
}
}
index.tsx
import Create from "./create";
import PostList from "./feed";
const Home = () => {
return(
<div>
<h1 className="text-blue absolute inset-y-7 left-7 text-xl font-semibold mb-20">Home</h1>
<Create />
<PostList />
</div>
);}
export default Home
【问题讨论】:
-
PostList 是一个页面,而不是一个组件。所以你不能在另一个页面中使用它
标签: reactjs next.js react-component getserversideprops