本文介绍了在非构造函数中,直接超级调用是非法的,而是使用super。“constructor”()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!


  • 我是试图写简单点击事件功能的新人。

  • I am new to react am trying to write a simple click event functionality.

具有此类名的span标签

  • 我在codepen中写了一个小原型,它在那里工作正常...

  • 但如果我合并代码在我的代码库它抛出一个错误...

  • 你们知道什么问题

  • 即使在codepen它显示错误
    未知:在类构造函数(16:8)之外的super()

  • When I click choice the span tag with this class name

  • I wrote a small prototype in codepen and its working fine there...
  • but if i incorporate the same code in my codebase its throwing an error...
  • do you guys know what is the problem
  • even in codepen it shows the errorunknown: super() outside of class constructor (16:8)

  • 尝试修复错误的实际代码库

     c>)中的错误将被抛出:'this'不允许在super()之前:

    UPDATE: If one forgets to call super(props) in constructor(), but then attempts to access this, the following error will be thrown: 'this' is not allowed before super():

    Module build failed: SyntaxError: C:/_workspaces/hello-world/some-component.jsx: 'this' is not allowed before super()
      20 | class SomeComponent extends React.Component {
      21 |     constructor(props) {
    > 22 |         this.state = {
         |         ^
      23 |             message: 'hello world'
      24 |         }
      25 |     }
    

    这里有一些关于为什么需要它的信息:

    Here's a little more info on why it's required: https://discuss.reactjs.org/t/should-we-include-the-props-parameter-to-class-constructors-when-declaring-components-using-es6-classes/2781

    ES6静态没有内部方法,只是一个隐含的 render()

    ES6 Static No internal methods, just an implied render()

    import React from 'react';
    
    const SomeComponent = ({
        message
    }) => (
        <div>{message}</div>
    );
    
    SomeComponent.propTypes = {};
    
    export default SomeComponent;
    

    这篇关于在非构造函数中,直接超级调用是非法的,而是使用super。“constructor”()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    1403页,肝出来的..

    09-06 12:49