Uncaught (in promise) TypeError: Cannot call a class as a function
Uncaught (in promise) TypeError: Cannot call a class as a function
我最近将我的 babel 版本升级到了最新版本。更新后,正常的 class 在我的 React 应用程序中无法被 babel 识别,并且出现以下错误。
Uncaught (in promise) TypeError: Cannot call a class as a function
.babelrc
{
"presets": [
"env",
"react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
我的应用程序中与 babel 相关的库:
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1"
babel 不理解这个class
CRUDTree.js
const resourceBoxWidth = 225;
const resourceBoxHeight = 85;
const halfBoxWidth = resourceBoxWidth / 2;
const halfBoxHeight = resourceBoxHeight / 2;
const urlLeftMargin = 10;
const urlFontSize = 12;
const fullPathFontSize = 10;
export default class{
static resourceBoxWidth() { return resourceBoxWidth; }
static resourceBoxHeight() { return resourceBoxHeight; }
static halfBoxHeight() { return halfBoxHeight; }
static halfBoxWidth() { return halfBoxWidth; }
}
APITree.js
import React, { Component } from 'react';
import CRUDTree from './CRUDTree';
class extends Component{
render(){
return(
<CRUDTree data={
[
this.state.treedata,
this.onClick,
{
x: this.state.offsetX,
y: this.state.offsetY
}
]}
width={400} height={500}
options={{
border: "2px solid black",
margin: {
top: 0,
bottom: 0,
left: 50,
right: 0
}
}
} />
)
}
}
如果您的 CRUDTree
是一个 React 组件(在我看来就是这样),那么您的定义是错误的。您缺少 extends
部分。
export default class extends React.Component {
....
}
我最近将我的 babel 版本升级到了最新版本。更新后,正常的 class 在我的 React 应用程序中无法被 babel 识别,并且出现以下错误。
Uncaught (in promise) TypeError: Cannot call a class as a function
.babelrc
{
"presets": [
"env",
"react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
我的应用程序中与 babel 相关的库:
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1"
babel 不理解这个class
CRUDTree.js
const resourceBoxWidth = 225;
const resourceBoxHeight = 85;
const halfBoxWidth = resourceBoxWidth / 2;
const halfBoxHeight = resourceBoxHeight / 2;
const urlLeftMargin = 10;
const urlFontSize = 12;
const fullPathFontSize = 10;
export default class{
static resourceBoxWidth() { return resourceBoxWidth; }
static resourceBoxHeight() { return resourceBoxHeight; }
static halfBoxHeight() { return halfBoxHeight; }
static halfBoxWidth() { return halfBoxWidth; }
}
APITree.js
import React, { Component } from 'react';
import CRUDTree from './CRUDTree';
class extends Component{
render(){
return(
<CRUDTree data={
[
this.state.treedata,
this.onClick,
{
x: this.state.offsetX,
y: this.state.offsetY
}
]}
width={400} height={500}
options={{
border: "2px solid black",
margin: {
top: 0,
bottom: 0,
left: 50,
right: 0
}
}
} />
)
}
}
如果您的 CRUDTree
是一个 React 组件(在我看来就是这样),那么您的定义是错误的。您缺少 extends
部分。
export default class extends React.Component {
....
}