[英]Error occurs when setting the current index of a react-virtualized List as state
Problem 问题
I'm trying to put startIndex
in state from within onRowsRendered()
. 我正在尝试从
onRowsRendered()
内部将startIndex
置于状态。
This works fine, until CellMeasurer
is put into the mix. 这很好, 直到将
CellMeasurer
放入混合液中为止 。
When scrolling down and then up, the following error occurs: 向下滚动然后向上滚动时,会出现以下错误:
Uncaught Invariant Violation: Maximum update depth exceeded.
未捕获的恒定违反:超出最大更新深度。 This can happen when a component repeatedly calls
setState
insidecomponentWillUpdate
orcomponentDidUpdate
.当组件重复调用
componentWillUpdate
或componentDidUpdate
内部的setState
时,可能会发生这种情况。 React limits the number of nested updates to prevent infinite loops.React限制了嵌套更新的数量,以防止无限循环。
What causes this problem and what solves it? 是什么导致了这个问题,又是什么解决了?
Demo 演示版
CellMeasurer
(error) CellMeasurer
(错误) CellMeasurer
(no error) CellMeasurer
(无错误) Code 码
import React from "react"; import ReactDOM from "react-dom"; import faker from "faker"; import { List, CellMeasurer, CellMeasurerCache } from "react-virtualized"; import "./styles.css"; faker.seed(1234); const rows = [...Array(1000)].map(() => faker.lorem.sentence(faker.random.number({ min: 5, max: 10 })) ); const App = () => { const [currentIndex, setCurrentIndex] = React.useState(0); const rowRenderer = ({ key, index, style, parent }) => { return ( <div style={style}> <div style={{ borderBottom: "1px solid #eee", padding: ".5em 0" }}> {rows[index]} </div> </div> ); }; return ( <> <h1>{currentIndex}</h1> <p> <em>When scrolling down and then up, an error occurs. Why?</em> </p> <List height={400} width={600} rowCount={rows.length} rowHeight={35} rowRenderer={rowRenderer} style={{ outline: "none" }} onRowsRendered={({ startIndex }) => { setCurrentIndex(startIndex); }} /> </> ); }; const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
You need to move rowRenderer
and cellMeasurer
function outside of your Functional Component. 您需要将
rowRenderer
和cellMeasurer
函数移到功能组件之外。 Because It will be recreated every time your functional component is rendered. 因为它将在每次渲染功能组件时重新创建。
Functional Component: https://codesandbox.io/s/nnp9z3o9wj?fontsize=14 功能组件: https : //codesandbox.io/s/nnp9z3o9wj?fontsize=14
Or you can use Class Component: 或者您可以使用类组件:
import React from "react";
import ReactDOM from "react-dom";
import faker from "faker";
import { List, CellMeasurer, CellMeasurerCache } from "react-virtualized";
import "./styles.css";
faker.seed(1234);
const rows = [...Array(1000)].map(() =>
faker.lorem.sentence(faker.random.number({ min: 5, max: 10 }))
);
class VirtualList extends React.Component {
rowRenderer = ({ key, index, style, parent }) => {
return (
<div style={style}>
<div style={{ borderBottom: "1px solid #eee", padding: ".5em 0" }}>
{rows[index]}
</div>
</div>
);
};
render() {
return (
<List
height={400}
width={600}
rowCount={rows.length}
rowHeight={35}
rowRenderer={this.rowRenderer}
style={{ outline: "none" }}
onRowsRendered={this.props.setCurrentIndex}
/>
)
}
}
const App = () => {
const [currentIndex, setCurrentIndex] = React.useState(0);
return (
<>
<h1>{currentIndex}</h1>
<p>
<em>When scrolling down and then up, an error occurs. Why?</em>
</p>
<VirtualList setCurrentIndex={setCurrentIndex} />
</>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.