在React中呈现任务列表,数组问题。

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

render task list in react, problem with array

问题

以下是要翻译的代码部分:

function TaskList() {
  const { tasks } = useContext(TaskContext);

  return tasks.length === 0 ? (
    <h2>No hay tareas agregadas</h2>
  ) : (
    <section className="task-list">
      <div className="pending-task">
        <h3>Tareas pendientes:</h3>
        <ul className="list">
          {tasks.filter((t) => t.done === false).length === 0 ? (
            <h4>No hay tareas por realizar</h4> // 如果没有要做的任务应该呈现这个
          ) : (
            tasks
              .filter((t) => t.done === false)
              .map((t, i) => <TaskCard key={t.id} task={t} index={i} />) // 这里呈现了待办任务
          )}
        </ul>
      </div>
      <div className="done-task">
        <h3>Tareas realizadas:</h3>
        <ul className="list">
          {tasks.filter((t) => t.done === true).length === 0 ? (
            <h4>No ha realizado ninguna tarea</h4> // 如果没有已完成的任务我仍然想要显示这个
          ) : (
            tasks
              .filter((t) => t.done === true)
              .map((t, i) => <TaskCard key={t.id} task={t} index={i} />) // 这里呈现了已完成的任务
          )}
        </ul>
      </div>
    </section>
  );
}

请注意,这是代码的中文翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时提出。

英文:

I have a problem wanting to conditionally render a task group depending on whether they are marked as performed or pending.

function TaskList() {
const { tasks } = useContext(TaskContext);
return tasks.length === 0 ? (
<h2>No hay tareas agregadas</h2>
) : (
<section className="task-list">
<div className="pending-task">
<h3>Tareas pendientes:</h3>
<ul className="list">
{tasks.filter((t) => t.done === false).length === 0 ? (
<h4>No hay tareas por realizar</h4> // In case there is no task to do, you should render this
) : (
tasks
.filter((t) => t.done === false)
.map((t, i) => <TaskCard key={t.id} task={t} index={i} />) //here are rendered those that are pending
)}
</ul>
</div>
<div className="done-task">
<h3>Tareas realizadas:</h3>
<ul className="list">
{tasks.filter((t) => t.done === true).length === 0 ? (
<h4>No ha realizado ninguna tarea</h4> //In case no task has been performed I still want to show this
) : (
tasks
.filter((t) => t.done === true)
.map((t, i) => <TaskCard key={t.id} task={t} index={i} />) //here are rendered the ones I already made
)}
</ul>
</div>
</section>
);
}

But when I put it to the test and click the button to mark the task as done, I throw the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'done') at TaskList.jsx:16:34

What would be this line here:

tasks.filter((t) => t.done === false).length === 0

I am aware that this error can occur because when filtering an array without elements, you cannot find the specified done property.

the funny thing is that after the app has been bugled due to the aforementioned error, if I refresh the page, the app works again and the task in which I clicked appears marked, that is, the bug is deleted, so I don’t know what I’m doing wrong, I have a couple of months practicing with react and there are things I’m learning.

答案1

得分: 1

这是我的上下文的一部分:

const [tasks, setTask] = useState([]);

const [checked, setChecked] = useState(false);

useEffect(() => setTask(data), []);

// 这里的 *data* 值来自一个导入,它是一个数组,读取浏览器本地存储中存储的任务
英文:

This is part of my context:

const [tasks, setTask] = useState([]);
const [checked, setChecked] = useState(false);
useEffect(() => setTask(data), []); 
// Here the value of *data* comes from an import that is an array that reads the tasks stored in the browser localstorage

答案2

得分: 0

如果您确定TaskContext中的tasks数组已正确初始化并包含具有done属性的有效任务对象,并且在呈现TaskList组件时tasks数组不为null或未定义,您可以在呈现组件之前添加一个空检查以处理这种情况。

if (!tasks) {
  return null // 或者呈现加载状态
}
英文:

If you are sure that that the tasks array in the TaskContext is properly initialized and contains valid task objects with the done property and tasks array is not null or undefined when the TaskList component is rendered. You can add a null check before rendering the component to handle such cases.

if (!tasks) {
  return null // or render a loading state
}

答案3

得分: 0

你可以将这一行修改为

tasks.some((t) => !t?.done)
英文:

You can modify this line

tasks.filter((t) => t.done === false).length === 0

with

tasks.some((t) => !t?.done)

huangapple
  • 本文由 发表于 2023年7月11日 01:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656067.html
匿名

发表评论

匿名网友

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

确定