我不明白为什么react-window 会在每个(所有) useState events 上重新渲染列表,这些值甚至没有连接到列表本身。有人可以解释一下这是如何在内部链接的,以及如何避免那些重新渲染?
import React, { useState } from "react";
import { FixedSizeList, areEqual } from "react-window";
function App() {
const [count, setCount] = useState(0);
const handleOnClick = () => {
console.log("PUSHED ME");
setCount(count+1) // why does it trigger a re-render of FixedSizeList ?
}
const Row = React.memo(props => {
const { index, style } = props;
return <div style={style}>Row {index}</div>;
}, areEqual);
return (
<div className="App">
<FixedSizeList
height={200}
itemCount={100}
itemSize={50}
width={'100%'}
>
{Row}
</FixedSizeList>
<div onClick={handleOnClick}>Push Me</div>
</div>
);
}
export default App;
我还尝试使用第二个 react-window areEqual 参数,但没有帮助。https://react-window.vercel.app/#/api/areEqual
任何关于如何解决这个问题的提示都非常感谢!提前谢谢了!