Call a static function into the class React ES6

前端 未结 2 414
终归单人心
终归单人心 2020-12-15 03:28

I have the following ReactJS class:

import React from \'react\'

export class Content extends React.Component {

  static getValue(key) {
    return key
  }
         


        
相关标签:
2条回答
  • 2020-12-15 03:40

    A static method needs to be accessed on the class not an instance. So in your case, use:

    Content.getValue()

    However, a static method won't be able to access this -- I don't think you want the method to be static based on your code sample above.

    More: Static Members in ES6

    0 讨论(0)
  • You can access from within the class as this.constructor.getValue.

    Edit: I've added a JSFiddle here. The only change I made was adding the function call from the constructor and removing the dangerously set innerHTML - As shown, you can access the getValue static from this.constructor, and works just fine.

    0 讨论(0)
提交回复
热议问题