[英]too many re re-renders error on remove todo
import React, { useState, useEffect } from "react";
import "./style.css";
function App() {
const [movieData, setMovieData] = useState();
const [directorData, setDirectorData] = useState();
const [listData, setListData] = useState([]);
const submitHandler = () => {
setListData([
...listData,
{
movie: movieData,
director: directorData,
},
]);
setMovieData("");
setDirectorData("");
};
const removeHandler = (id) => {
const newlist = listData.filter((remId) => {
return remId !== id;
});
setListData(newlist)
};
return (
<div className="App">
<div>
<h1>Todo App</h1>
</div>
<div className="form">
<div>
<label>Movie</label>
<input
type="text"
placeholder="type items"
value={movieData}
onChange={(e) => setMovieData(e.target.value)}
/>
</div>
<div>
<label>Director</label>
<input
type="text"
placeholder="type items"
value={directorData}
onChange={(e) => setDirectorData(e.target.value)}
/>
</div>
<div>
<button onClick={submitHandler}>Submit</button>
</div>
</div>
<div>
{listData.map((item, index) => {
return (
<li key={index}>
<span>{item.movie}</span>, <span>{item.director}</span>{" "}
<span style={{ marginLeft: "2rem" }}>
<button onClick={removeHandler(index)} style={{ color: "red" }}>
X
</button>
</span>
</li>
);
})}
</div>
</div>
);
}
export default App;
As soon as I added removeHandler, I am getting too many re renders onSubmit.一旦我添加了 removeHandler,我就会在提交时收到太多重新渲染。 Its working fine once I remove removeHandler.
一旦我删除 removeHandler,它就可以正常工作。
As soon as I added removeHandler, I am getting too many re renders onSubmit.一旦我添加了 removeHandler,我就会在提交时收到太多重新渲染。 Its working fine once I remove removeHandler.
一旦我删除 removeHandler,它就可以正常工作。 As soon as I added removeHandler, I am getting too many re renders onSubmit.
一旦我添加了 removeHandler,我就会在提交时收到太多重新渲染。 Its working fine once I remove removeHandler.
一旦我删除 removeHandler,它就可以正常工作。
Change to onClick={() => removeHandler(index)}
更改为
onClick={() => removeHandler(index)}
and you also have to remove 'return' in your array.filter并且您还必须删除 array.filter 中的“return”
remId !== id;
});
This happens because you're not passing an arrow function to onClick. You're writing onClick={removeHandler(index)}
instead of onClick={() => removeHandler(index)}
.发生这种情况是因为您没有将箭头 function 传递给 onClick。您正在编写
onClick={removeHandler(index)}
而不是onClick={() => removeHandler(index)}
。
It's because functionName(param)
syntax is generally used to call functions so when you write that, it causes infinite renders.这是因为
functionName(param)
语法通常用于调用函数,所以当您编写它时,它会导致无限渲染。
use this function some new changes使用这个 function 一些新的变化
const removeHandler = (id) => { const newlist = [...listData]; const removeHandler = (id) => { const newlist = [...listData]; newlist.splice(id, 1);
newlist.splice(id, 1); setListData(newlist);
setListData(新列表); };
};
and change onclick event并更改 onclick 事件
onClick={() => removeHandler(index)} onClick={() => removeHandler(index)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.