【发布时间】:2018-05-06 12:24:44
【问题描述】:
运行时出现意外。我正在尝试加载反应虚拟化的 InfiniteLoader。想知道如何通过这个组件调用 API 如果有任何示例可用。我从https://github.com/bvaughn/react-virtualized/blob/master/docs/InfiniteLoader.md 获取了组件 我正在使用来自https://bvaughn.github.io/react-virtualized/#/components/InfiniteLoader的示例
import React from 'react';
import ReactDOM from 'react-dom';
import { InfiniteLoader, List } from 'react-virtualized';
import 'react-virtualized/styles.css'; // only needs to be imported once
export default class MyList extends React.PureComponent {
const remoteRowCount
const list = [];
function isRowLoaded ({ index }) {
return !!list[index];
}
function loadMoreRows ({ startIndex, stopIndex }) {
}
function rowRenderer ({ key, index, style}) {
return (
<div
key={key}
style={style}
>
{list[index]}
</div>
)
}
//Render the list from this function
render() {
return(
<InfiniteLoader
isRowLoaded={isRowLoaded}
loadMoreRows={loadMoreRows}
rowCount={remoteRowCount}
>
{({ onRowsRendered, registerChild }) => (
<List
height={200}
onRowsRendered={onRowsRendered}
ref={registerChild}
rowCount={remoteRowCount}
rowHeight={20}
rowRenderer={rowRenderer}
width={300}
/>
)}
</InfiniteLoader>
);
}
}
得到以下给定的异常
Module build failed: SyntaxError: D:/code/react-starter/src/Components/MyList/MyList.js: Unexpected token (8:8)
6 | export default class MyList extends React.PureComponent {
7 |
> 8 | const remoteRowCount
| ^
9 |
10 | const list = [];
11 |
【问题讨论】:
-
这是因为您在组件中声明了
remoteRowCount和list。将它们放在类定义之上(或者如果您希望它们成为组件的一部分,请在构造函数中正确添加它们) -
我想知道这些规则,我在哪里可以找到它们?
-
看看 React 文档,它向您展示了如何设置组件。为了在构造函数中向组件添加属性(作为实际组件的对象属性或状态值),请查看生命周期和构造函数:reactjs.org/docs/state-and-lifecycle.html
-
谢谢,但是现在,看到这个组件工作的解决方案是什么
-
就像我在第一条评论中所说的,将它们移出你的类定义