【发布时间】:2023-04-09 21:32:01
【问题描述】:
我是新手,正在尝试编写一个简单的点击事件功能。
当我点击选择这个类名的 span 标签时
- 我在 codepen 中编写了一个小型原型,它在那里工作正常...
- 但如果我在我的代码库中加入相同的代码,则会引发错误...
- 你们知道是什么问题吗
- 即使在 codepen 中也会显示错误 未知:类构造函数之外的 super() (16:8)
工作的小型原型代码 http://codepen.io/anon/pen/rxPvyr?editors=0110
尝试修复错误的实际代码库 http://codepen.io/kk-/pen/BjeLry
Line 16: Direct super call is illegal in non-constructor, use super."constructor"() instead
14 |
15 | constructor(props) {
> 16 | super(props);
| ^
17 |
18 | this.handleClick = this.handleClick.bind(this);
【问题讨论】:
-
为什么在使用
React.createClass时要定义constructor?createClass模式会为您自动绑定。 -
@mathletics 感谢您的回复...您可以在 codepen 中更新它如此混乱:(
-
阅读文档:facebook.github.io/react/docs/component-specs.html;您将 ES6 类模式与 React 工厂模式混为一谈。
-
@mathletics 我正在阅读它......如果你更新 codepen 会很棒
标签: javascript html css reactjs