reactjs - how to call a React function in a loop without an event?
问题描述
React code: I'm trying to use a simple counter in a loop to count each of my todo list items, but I can't seem to execute the code in React. I simply need countopen and countcomplete to get populated and then display them on screen.
countopen and countcomplete are both set in the state, I thought that would be easiest...
App.js ...
//I tried putting my code out in a separate block, then calling it but that doesn't work
todos.map((todo, index) => {
if (todo[index].completed === 0) {
setCountopen(countopen + 1);
} else {
setCountcomplete(countcomplete + 1);
}})
}
<div className="App">
<div className="todo-list">
{todos.map((todo, index) => (
<Todo key={index} index={index} todo={todo} completeTodo={completeTodo} tallyTodos={tallyTodos} />
//i tried putting my code here, no dice React throws syntax errors
**if (todo[index].completed === 0) {
setCountopen(countopen + 1);
} else {
setCountcomplete(countcomplete + 1);
))}**
Open: {countopen} Closed: {countcomplete}
</div>
</div>
);
So I suppose the question to answer is: How do I code my if/else code to get looped over in React? I thought I could piggy back my .map function but nope. I simply need to tally the 2 types of todos and display them in {countopen} and {countcomplete}
Thanks in advance!
解决方案
This is a derived state and it is best to avoid replicating that in the state as this is prone to desynchronization with the source state value. You should compute it on the fly in the render
or if the computation is expensive, use a caching strategy like useMemo
React hook.
Before the return you can compute the values using a for
loop or compute the length of the filtered todo list that includes only the completed items.
The countopen
is always just the difference between todos count and number of completed items - a simple const
is enough.
Please see the example below:
let countcomplete = 0;
for (const todo of todos) {
if (todo.completed !== 0) {
countcomplete++;
}
}
// --or--
const completedTodos = todos.filter(todo => todo.completed !== 0);
countcomplete = completedTodos.length;
const countopen = todos.length - countcomplete;
return (
<div className="App">
<div className="todo-list">
{todos.map((todo, index) => (
<Todo key={index} index={index} todo={todo} completeTodo={completeTodo} tallyTodos={tallyTodos} />
)}
Open: {countopen} Closed: {countcomplete}
</div>
</div>
);
推荐阅读
- c++ - 如何解决 C++ 中的 VS 代码错误 includePath?
- python - SSL_ERROR_SSL GCP Pub/Sub 发布时出错
- xcode - Xcode 错误“无法创建保存面板”。创建新的 Swift 文件时
- r - 如何使用 keras 在 r 中为简单的神经网络编写模型定义?
- c# - C# 调用具有不同类型的相同扩展函数作为参数(可能是委托?)
- java - 有没有一种方法可以用来管理 Java 中字符串的空间?
- sql - 在 Oracle 的过程中运行 SQL 查询时出错
- c# - 解码从医疗设备迈瑞串口接收到的字符
- java - 单元测试:如何验证被测试的 void 方法是否调用了具有正确参数的另一个方法?
- reactjs - 我怎样才能让这个函数接受一个数组元素的 id,然后调度一个动作?MERN 堆栈