【问题标题】:Unexpected token with react带有反应的意外令牌
【发布时间】: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 |

【问题讨论】:

  • 这是因为您在组件中声明了remoteRowCountlist。将它们放在类定义之上(或者如果您希望它们成为组件的一部分,请在构造函数中正确添加它们)
  • 我想知道这些规则,我在哪里可以找到它们?
  • 看看 React 文档,它向您展示了如何设置组件。为了在构造函数中向组件添加属性(作为实际组件的对象属性或状态值),请查看生命周期和构造函数:reactjs.org/docs/state-and-lifecycle.html
  • 谢谢,但是现在,看到这个组件工作的解决方案是什么
  • 就像我在第一条评论中所说的,将它们移出你的类定义

标签: reactjs react-virtualized


【解决方案1】:

你可以试试下面的代码。

基本上,我:

  1. 已将 listremoteRowCount 移至 MyList 的状态。
  2. isRowLoaded loadMoreRows rowRenderer 更改为 MyList 的实例方法。您可能还想对onRowsRendered 等做同样的事情。

    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 {
      constructor() {
        super();
    
        this.state = {
          remoteRowCount: 0,
          list: [],
        };
    
        this.isRowLoaded = this.isRowLoaded.bind(this);
        this.loadMoreRows = this.loadMoreRows.bind(this);
        this.rowRenderer = this.rowRenderer.bind(this);
      }
    
      isRowLoaded ({ index }) {
        return !!this.state.list[index];
      }
    
      loadMoreRows ({ startIndex, stopIndex }) {
      }
    
      rowRenderer ({ key, index, style}) {
        return (
          <div
            key={key}
            style={style}
          >
            {this.state.list[index]}
          </div>
        )
      }
    
      //Render the list from this function
      render() {
        return(
          <InfiniteLoader
        isRowLoaded={this.isRowLoaded}
        loadMoreRows={this.loadMoreRows}
        rowCount={this.state.remoteRowCount}
      >
        {({ onRowsRendered, registerChild }) => (
          <List
            height={200}
            onRowsRendered={onRowsRendered}
            ref={registerChild}
            rowCount={remoteRowCount}
            rowHeight={20}
            rowRenderer={this.rowRenderer}
            width={300}
          />
        )}
      </InfiniteLoader>
        );
      }
    
    }
    

【讨论】:

  • 非常感谢
  • 在哪里可以找到关于 react 的优秀教程
  • 非常感谢,我想知道这个例子中的数据是从哪里进入网格的,我看不到任何api
  • 相关代码为here
猜你喜欢
  • 1970-01-01
  • 2016-12-28
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多