如何将传递给客户端组件的 prop 映射到 NEXT 13 中?

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

How to map prop passed to client component in NEXT 13?

问题

Here's the translated code part you requested:

我对下面这个13个应用程序目录还很陌生当我像这样在服务器组件中映射时它能正常工作

const Tasks = async () => {
  const { tasks } = await getAllTasks()
  
  return (
    <section>
      <ul className="flex flex-col mx-3">
        {tasks?.map(({_id, title, body}: TaskProps) => (
          <Task key={_id} id={_id} title={title} body={body} />
        ))}
      </ul>
    </section>
  )
}

export default Tasks

尽管当我尝试将此代码提取到客户端组件中以便我可以使用 useEffect我无法映射传递的 'tasks' 属性

"use client"
//我的导入在这里

export default function TaskList (tasks) {
      return (
          <ul>
            {tasks.map((task) => (
              <Task key={_id} id={_id} title={title} body={body} />
            ))}
          </ul>
      )
    }

Task.tsx:

export default function Task({ id, title, body }: TaskProps) {
  async function deleteTask() {
    await removeTask(title);
  }
  return (
    <li key={id}>
      <div className="flex justify-between" >
        <h1 className='font-bold'>{title}</h1>
        <span 
          className="material-icons hover:cursor-pointer text-skin-dark" 
          onClick={deleteTask}
        >delete</span>
      </div>
      <a className="mr-12" >{body}</a>
    </li>
  );
}

Please note that I've retained the original structure and formatting of the code while providing the translation.

英文:

I'm new to the next 13 APP directory and it works when I map in the server component like this:

const Tasks = async () =&gt; {
const { tasks } = await getAllTasks()
return (
&lt;section&gt;
&lt;ul className=&quot;flex flex-col mx-3&quot;&gt;
{tasks?.map(({_id, title, body}: TaskProps) =&gt; (
&lt;Task key={_id} id={_id} title={title} body={body} /&gt;
))}
&lt;/ul&gt;
&lt;/section&gt;
)
}
export default Tasks

Though when I try to extract this code into a client component (so that I can useEffect), I'm unable to map the passed 'tasks' prop

 &quot;use client&quot;
//my imports here
export default function TaskList (tasks) {
return (
&lt;ul&gt;
{tasks.map((task) =&gt; (
&lt;Task key={_id} id={_id} title={title} body={body} /&gt;
))}
&lt;/ul&gt;
)
}

Task.tsx:

  export default function Task({ id, title, body }: TaskProps) {
async function deleteTask() {
await removeTask(title);
}
return (
&lt;li key={id}&gt;
&lt;div className=&quot;flex justify-between&quot; &gt;
&lt;h1 className=&#39;font-bold&#39;&gt;{title}&lt;/h1&gt;
&lt;span 
className=&quot;material-icons hover:cursor-pointer text-skin-dark&quot; 
onClick={deleteTask}
&gt;delete&lt;/span&gt;
&lt;/div&gt;
&lt;a className=&quot;mr-12&quot; &gt;{body}&lt;/a&gt;
&lt;/li&gt;
);
}

I've tried a conditional return for the tasks prop 'tasks && ( JSX )'
Also tried putting the list item tags around Task, inside Tasks

Appreciate any assistance 如何将传递给客户端组件的 prop 映射到 NEXT 13 中?

答案1

得分: 1

你是如何将 tasks 传递到客户端组件中的?还看起来你可能忘记了解构客户端组件的 props。你写了

"use client";
// 这里是我的导入

export default function TaskList(tasks) {
  return (
    &lt;ul&gt;
      {tasks.map((task) =&gt; (
        &lt;Task key={_id} id={_id} title={title} body={body} /&gt;
      ))}
    &lt;/ul&gt;
  );
}

tasks 应该是对象的属性,像这样:

"use client";
// 这里是我的导入

export default function TaskList({ tasks }) {
  return (
    &lt;ul&gt;
      {tasks.map((task) =&gt; (
        &lt;Task key={_id} id={_id} title={title} body={body} /&gt;
      ))}
    &lt;/ul&gt;
  );
}
英文:

How are you passing tasks into your client component from your server component? It also looks like you might have forgotten to destructure your client component's props. You wrote

&quot;use client&quot;;
//my imports here

export default function TaskList(tasks) {
  return (
    &lt;ul&gt;
      {tasks.map((task) =&gt; (
        &lt;Task key={_id} id={_id} title={title} body={body} /&gt;
      ))}
    &lt;/ul&gt;
  );
}

but tasks should be a property on an object, like this:

&quot;use client&quot;;
//my imports here

export default function TaskList({ tasks }) {
  return (
    &lt;ul&gt;
      {tasks.map((task) =&gt; (
        &lt;Task key={_id} id={_id} title={title} body={body} /&gt;
      ))}
    &lt;/ul&gt;
  );
}

huangapple
  • 本文由 发表于 2023年5月17日 09:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268041.html
匿名

发表评论

匿名网友

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

确定