Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a static function into the class React ES6

I have the following ReactJS class:

import React from 'react'  export class Content extends React.Component {    static getValue(key) {     return key   }    render() {     let value = this.getValue(this.props.valueKey);     return <span dangerouslySetInnerHTML={{__html: value}} />   } } 

But I have the following error:

TypeError: this.getValue is not a function 

I don't understand. Is this the good way to call a static function? I think react is doing something with statics, but I don't know what.

like image 922
Ajouve Avatar asked Feb 27 '16 16:02

Ajouve


1 Answers

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

like image 177
Cymen Avatar answered Oct 14 '22 19:10

Cymen