javascript - 元素类型无效:未定义 - Reactjs
问题描述
我有以下错误:Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of 'Screens/Enjoy.js'.
使用以下代码:
import React, {Component, Fragment} from 'react';
import ReactDOM from 'react-dom';
import { renderToString, AMP } from 'react-amp-template'
class Enjoy extends Component {
render() {
const { Title, Link, Carousel } = AMP;
const amppage = (
<Fragment>
<Title>Hello guys :)</Title>
<Link rel="canonical" href="http://localhost" />
<h1>Hello World</h1>
<Carousel lightbox width="400" height="300" layout="responsive" type="slides">
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
</Carousel>
</Fragment>
);
return(
amppage
)
}
}
export default Enjoy
我正在使用“react”:“15.3.1”和“react-dom”:“15.3.1”
请告诉我,可能是什么问题?我查看了类似的错误,但他们的解决方案对我没有帮助。提前非常感谢!
upd:非常抱歉,我render()
在代码中复制的时候漏掉了方法!
解决方案
您不能将常量声明放在类体内。要么将常量移到类之外,要么像我所做的那样将属性定义为类属性。
此外,单词default的拼写错误。也更正一下
选项1:
const amppage = (
<Fragment>
<Title>Hello guys :)</Title>
<Link rel="canonical" href="http://localhost" />
<h1>Hello World</h1>
<Carousel lightbox width="400" height="300" layout="responsive" type="slides">
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
</Carousel>
</Fragment>
);
class Enjoy extends Component {
render() {
return amppage;
}
}
export default Enjoy;
选项 2:
class Enjoy extends Component {
amppage = (
<Fragment>
<Title>Hello guys :)</Title>
<Link rel="canonical" href="http://localhost" />
<h1>Hello World</h1>
<Carousel lightbox width="400" height="300" layout="responsive" type="slides">
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
<amp-img src="image.jpg" width="400" height="300" layout="responsive"></amp-img>
</Carousel>
</Fragment>
);
render() {
return this.amppage;
}
}
export default Enjoy;
推荐阅读
- java - 如何解决错误:当我尝试在 Java 中实例化单例类时,“MyClass.INSTANCE”不是语句?
- ajax - PhoneGap Ajax API 请求没有响应
- r - 分类/天气预报
- arrays - 如何在适用时将可以是领域列表或字符串的任何内容转换为领域列表
- c# - 将输入值与 IF 语句 C# ASP.NET Razor 中的字符串进行比较
- javascript - 函数不会运行代码的第二部分我该如何解决这个问题?
- mysql - 哈希表的 MySQL 模式
- c# - 如何从excel中的合并单元格中获取范围
- javascript - “typeof window”类型上不存在属性“location”
- r - piewise线性回归集约束