GetServerSideProps似乎没有被调用。

huangapple go评论70阅读模式
英文:

GetServerSideProps doesn't seem to be called

问题

I can help you translate the code part into Chinese. Here it is:

为什么我看不到帖子我只能看到带有所有帖子的H1元素否则什么都不会发生在调试模式下我也看不到调用的URL我使用的是 Next.JS 13如果我尝试在REACT中执行整个操作当然不包括getServerSideProps()我没有问题 - 一切正常

Main.js

import React from "react";

export const getServerSideProps = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/posts");
  let allPosts = await res.json();

  return {
    props: {
      allPosts: allPosts.map((post) => post.title),
    },
  };
};

const Posts = ({ allPosts }) => {
  return (
    <div>
      <h1>所有帖子</h1>
      {allPosts?.map((post, idx) => (
        <div key={idx}>{post}</div>
      ))}
    </div>
  );
};

export default Posts;

layout.jsx

export default function RootLayout() {
  return (
    <html lang="en">
      <head>
        <title>Digital</title>
      </head>
      <body>
        <Main></Main>
      </body>
    </html>
  );
}

请注意,我只翻译了代码部分,不包括问题和其他内容。如果您需要更多翻译,请告诉我。

英文:

Why can't I see the posts? All I see is the H1 element with All Posts. Otherwise nothing happens. In debug mode I don't see the URL being called either. I use Next.JS 13. If I try the whole thing in REACT, without getServerSideProps() of course, I have no problem - everything works.

Main.js:

import React from &quot;react&quot;;
export const getServerSideProps = async () =&gt; {
const res = await fetch(&quot;https://jsonplaceholder.typicode.com/posts&quot;);
let allPosts = await res.json();
return {
props: {
allPosts: allPosts.map((post) =&gt; post.title),
},
};
};
const Posts = ({ allPosts }) =&gt; {
return (
&lt;div&gt;
&lt;h1&gt;All Posts&lt;/h1&gt;
{allPosts?.map((post, idx) =&gt; (
&lt;div key={idx}&gt;{post}&lt;/div&gt;
))}
&lt;/div&gt;
);
};
export default Posts;

layout.jsx:

 export default function RootLayout() {
return (
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;title&gt;Digital&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;Main&gt;&lt;/Main&gt;
&lt;/body&gt;
&lt;/html&gt;
);

}

答案1

得分: 0

This code worked for me, try this:

这段代码对我有效,可以尝试使用:

const Home = ({ allPosts }) => {
  return (
    <div>
      {allPosts.map((post, index) => (
        <p key={index}>
          <span>{index + 1}: </span>
          {post}
        </p>
      ))}
    </div>
  );
};

export default Home;

export const getServerSideProps = async () => {
  const res = await fetch('https://jsonplaceholder.typicode.com/posts');
  let allPosts = await res.json();

  return {
    props: {
      allPosts: allPosts.map((post) => post.title),
    },
  };
};

Screenshot example

英文:

This code worked for me try this:

const Home = ({ allPosts }) =&gt; {
return (
&lt;div&gt;
{allPosts.map((post, index) =&gt; (
&lt;p key={index}&gt;
&lt;span&gt;{index + 1}: &lt;/span&gt;
{post}
&lt;/p&gt;
))}
&lt;/div&gt;
);
};
export default Home;
export const getServerSideProps = async () =&gt; {
const res = await fetch(&#39;https://jsonplaceholder.typicode.com/posts&#39;);
let allPosts = await res.json();
return {
props: {
allPosts: allPosts.map((post) =&gt; post.title),
},
};
};

Screenshot example

huangapple
  • 本文由 发表于 2023年4月19日 19:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定