【问题标题】:React Component - render return throwing syntax errorReact Component - 渲染返回抛出语法错误
【发布时间】:2021-07-26 15:41:30
【问题描述】:

对于 React 来说还是相当新的,有一个奇怪的语法问题。我正在创建一个 React 组件,我相信我有正确的打开/关闭 { & },但我想我一定是因为它抛出的错误而遗漏了一些东西?这是我的代码。

import React from 'react';
import { TreeList, SearchPanel, Scrolling, Lookup } from 'devextreme-react/tree-list';
import 'devextreme-react/text-area';
import 'whatwg-fetch';
import WireGrid from '../WireGrid.js'

const expandedRowKeys = [1];
// const allowedPageSizes = [5, 10, 15, 20];
class WireTree extends React.Component {
constructor(props) {
  super(props)
  this.state = {
    jsonData: null
  }
this.onFocusedRowChanged = this.onFocusedRowChanged.bind(this)
}

async componentDidMount() {
const url = "http://localhost:4741/wiretree";
const response = await fetch(url);
const data = await response.json();
    this.setState({
        'jsonData' : data.recordset
    })
}

onFocusedRowChanged(e) {
var rowData = e.row && e.row.data,
cellValue
if(rowData) {
  cellValue = e.component.cellValue(e.row.rowIndex, 'Filter');
  module.export = cellValue
  if (!cellValue) {
    return null;
  } else {
    console.log('cellValue: ', cellValue)
    // <WireGrid />
  }
}} 
}

render() {
  return (
    <TreeList
      id="wireTree"
      dataSource={this.state.jsonData}
      dataStructure="plain"
      rootValue=""
      defaultExpandedRowKeys={expandedRowKeys}
      columnAutoWidth={true}
      keyExpr="categoryID"
      parentIdExpr="ParentID"
      wordWrapEnabled={true}
      focusedRowEnabled={true}
      virtualModeEnabled={true}
      onFocusedRowChanged={this.onFocusedRowChanged}
    >
      <SearchPanel visible={true} />
      <Scrolling mode="standard" />
      <Lookup
        dataSource={this.state.jsonData}
        valueExpr="ID"
        displayExpr="Search" />
    </TreeList>
  );
}

export default WireTree;

这是它在控制台中显示的错误,它指向 'render() {' 行:

 ';' expected. ts(1005) [42, 10]

不是 ts = TypeScript 吗?有什么想法吗?

【问题讨论】:

  • 你在渲染函数之前的}太多了。
  • 使用好的编辑器,它会在检测到错误的确切位置提醒您。
  • @Taxel 哦,有趣 - 我以为你应该在组件之外调用渲染/返回?

标签: javascript reactjs react-hooks react-component


【解决方案1】:

在下面找到正确格式的代码

render 放置在组件之外,而不是关闭组件 } 在渲染之前将其移除并放置在渲染之后,如下所示

import React from 'react';
import { TreeList, SearchPanel, Scrolling, Lookup } from 'devextreme-react/tree-list';
import 'devextreme-react/text-area';
import 'whatwg-fetch';

const expandedRowKeys = [1];
class WireTree extends React.Component {
constructor(props) {
super(props)
this.state = {
jsonData: null
}
this.onFocusedRowChanged = this.onFocusedRowChanged.bind(this)
}

componentDidMount() {
fetch('http://localhost:4741/wiretree')
    .then((response) => response.json())
    .then(res => {
        this.setState({ jsonData: res });
    });
    }

 onFocusedRowChanged(e) {
 var rowData = e.row && e.row.data,cellValue
 if(rowData) {
 cellValue = e.component.cellValue(e.row.rowIndex, 'Filter');
 module.export = cellValue
 if (!cellValue) {
 return null;
  } else {
  console.log('cellValue: ', cellValue)
  }
 }} 

 render() {
 return (
 <TreeList
 id="wireTree"
 dataSource={this.state.jsonData}
 dataStructure="plain"
 rootValue=""
 defaultExpandedRowKeys={expandedRowKeys}
 columnAutoWidth={true}
 keyExpr="categoryID"
 parentIdExpr="ParentID"
 wordWrapEnabled={true}
 focusedRowEnabled={true}
 virtualModeEnabled={true}
 onFocusedRowChanged={this.onFocusedRowChanged}>

<SearchPanel visible={true} />
<Scrolling mode="standard" />
<Lookup
dataSource={this.state.jsonData}
valueExpr="ID"
displayExpr="Search" />

</TreeList>
);
}
}
export default WireTree;

【讨论】:

  • 谢谢!不幸的是,即使直接将您的代码复制/粘贴到我的代码上仍然会引发错误:&lt;TreeList ^ SyntaxError: Unexpected token '&lt;' 有什么想法吗?
  • 已编辑,您可以试试这段代码,如果您遇到任何语法错误,请告诉我
  • 刚刚用更新的代码再次尝试,不幸的是仍然得到同样的错误...... :( &lt;TreeList ^ SyntaxError: Unexpected token '&lt;'
【解决方案2】:

函数 onFocusedRowChanged 末尾的 { 太多了。 您还需要在底部导出之前使用 } 关闭您的课程。

【讨论】:

  • 谢谢!如果我在底部的导出之前添加},我的 linter(使用 VS Code)会在控制台中引发错误:Declaration or statement expected.。关于如何解决这个问题的任何想法?
  • 不客气。我不知道只是看到这样的代码。这可能是很多事情。可能是导入错误的组件,导出当前组件的方式出错,已用作全局变量等。但检查后我没有找到任何东西,对不起
猜你喜欢
  • 2019-11-15
  • 2016-03-29
  • 1970-01-01
  • 2020-03-12
  • 2020-11-27
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多