React TS 返回 undefined,然后返回数据

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

React TS is returning undefined then returns the data

问题

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

我正在使用无服务器框架和 tRPC 来处理路由当我调用后端时初始状态会是未定义然后返回适当的数据我不确定是前端调用有问题还是从后端检索数据有问题我尝试过添加等待但没有效果

路由调用
const t = initTRPC.create();

const appRouter = t.router({
  Todos: t.procedure
  .query(() => {
    // console.log(Todo.list())
    return Todo.list()
  })
})

数据库查询
export function list() {
    return PG.DB.selectFrom("todo")
    .selectAll()
    .orderBy("todoID")
    .execute()
}

前端
function IndexPage() {
  const todoList = trpc.Todos.useQuery()?.data;
  console.log("--------", todoList)
  if (!todoList) return <div>Loading...</div>;
  return (
    <div>
     <table className="table table-bordered">
              <thead>
                <tr>
                  <th className="th-sm">任务</th>
                </tr>
              </thead>
              <tbody>
                {todoList.map((todo) => {
                  return (
                    <>
                      <tr key={todo?.todoID}>
                        <td>{todo.task}</td>
                      </tr>
                    </>
                  );
                })}
              </tbody>
            </table>
    </div>
  );
}

希望这有助于您理解代码。如果您需要进一步的帮助,请随时提出。

英文:

I am using a serverless frame work and tRPC for routes. When I make a call to the backend it will initially be undefined and then return the appropriate data. I am not sure if my call is wrong on the frontend or if there is an issue with retrieving data from the backend. I have tried adding awaits, but nothing worked.

route call

const t = initTRPC.create();
const appRouter = t.router({
Todos: t.procedure
.query(() =&gt; {
// console.log(Todo.list())
return Todo.list()
})
})

db query

 export function list() {
return PG.DB.selectFrom(&quot;todo&quot;)
.selectAll()
.orderBy(&quot;todoID&quot;)
.execute()
}

frontend

function IndexPage() {
const todoList = trpc.Todos.useQuery()?.data;
console.log(&quot;--------&quot;, todoList)
if (!todoList) return &lt;div&gt;Loading...&lt;/div&gt;;
return (
&lt;div&gt;
&lt;table className=&quot;table table-bordered&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th className=&quot;th-sm&quot;&gt;task&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{todoList.map((todo) =&gt; {
return (
&lt;&gt;
&lt;tr key={todo?.todoID}&gt;
&lt;td&gt;{todo.task}&lt;/td&gt;
&lt;/tr&gt;
&lt;/&gt;
);
})}
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
);
}

答案1

得分: 2

代码部分不翻译。以下是翻译好的内容:

"It's normal for the data to initially be undefined. The query doesn't exist before the component first renders, so the component can't have the data yet (unless it's already in cache for some reason such as also being queried in another component).

If you want to create a component that has the data from a tRPC query as soon as it mounts the first time, you have several options, for example:

  • using server side rendering if your app allows this
  • creating a separate component that displays the data, and only rendering it if data exists

tRPC's useQuery uses Tanstack Query. You can see all of the things you can destructure from the query here: https://tanstack.com/query/v4/docs/react/reference/useQuery"

英文:

It's normal for the data to initially be undefined. The query doesn't exist before the component first renders, so the component can't have the data yet (unless it's already in cache for some reason such as also being queried in another component).

If you want to create a component that has the data from a tRPC query as soon as it mounts the first time, you have several options, for example:

  • using server side rendering if your app allows this
  • creating a separate component that displays the data, and only rendering it if data exists

tRPC's useQuery uses Tanstack Query. You can see all of the things you can destructure from the query here: https://tanstack.com/query/v4/docs/react/reference/useQuery

huangapple
  • 本文由 发表于 2023年2月8日 08:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380427.html
匿名

发表评论

匿名网友

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

确定