“Consolo.log shows I’m getting my items from the API, however they are not rendered on my page?”

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

Consolo.log shows I'm getting my items from api, however they are not rendered on my page?

问题

以下是您提供的代码的中文翻译部分:

这是我写的代码

import React from "react";
import { Todo } from "../../typings";

import Link from "next/link";

const fetchTodos = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/todos/");
  const todos: Todo[] = await res.json();
  console.log(todos);
  return todos;
};

async function TodoList() {
  const todos = await fetchTodos();
  return (
    <>
      {todos.map((todo) => {
        <p>
          <Link href={`/todos/${todo.id}`}>Todo: {todo.id}</Link>
        </p>;
      })}
    </>
  );
}

export default TodoList;

我在使用map函数来在我的页面上渲染项目时遇到了问题但我确实在控制台上看到了我的项目所以我认为问题可能出在map函数上不确定哪里出了问题

如果您需要任何其他翻译或帮助,请随时告诉我。

英文:

Here is the code that I wrote.

import React from &quot;react&quot;;
import { Todo } from &quot;../../typings&quot;;

import Link from &quot;next/link&quot;;

const fetchTodos = async () =&gt; {
  const res = await 

fetch(&quot;https://jsonplaceholder.typicode.com/todos/&quot;);
  const todos: Todo[] = await res.json();
  console.log(todos);
  return todos;
};

async function TodoList() {
  const todos = await fetchTodos();
  return (
    &lt;&gt;
      {todos.map((todo) =&gt; {
        &lt;p&gt;
          &lt;Link href={`/todos/${todo.id}`}&gt;Todo: {todo.id}&lt;/Link&gt;
        &lt;/p&gt;;
      })}
    &lt;/&gt;
  );
}

export default TodoList;

I have trouble using map to render the items on my page, but I do have my items logged on console, so I think it's just the map function. Not sure what's not working.

答案1

得分: 1

每当我们使用 map 函数时,都需要使用 return 语句,以便数据能够渲染,用户可以在页面上看到数据。

{todos.map((todo) => {
    return (
        <>
            <p>
                <Link href={`/todos/${todo.id}`}>任务{todo.id}</Link>
            </p>
        </>
    );
})}

如果不使用它,就什么都看不到 “Consolo.log shows I’m getting my items from the API, however they are not rendered on my page?” 希望这有所帮助!

英文:

Whenever we use map function always need to use the return statement so that data could render and user could see data on page.

{todos.map((todo) =&gt; {
     return (
     &lt;&gt;
        &lt;p&gt;
          &lt;Link href={`/todos/${todo.id}`}&gt;Todo: {todo.id}&lt;/Link&gt;
        &lt;/p&gt;
     &lt;/&gt;
     )
 })}

Without using it couldn't see anything “Consolo.log shows I’m getting my items from the API, however they are not rendered on my page?”

I hope this helps!

huangapple
  • 本文由 发表于 2023年5月8日 01:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195303.html
匿名

发表评论

匿名网友

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

确定