将一个静态函数调用到React ES6类中 [英] Call a static function into the class React ES6
本文介绍了将一个静态函数调用到React ES6类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下ReactJS类:
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}} />
}
}
但我有以下错误:
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.
推荐答案
需要访问静态方法这个类不是一个实例。所以在你的情况下,使用:
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.
更多: ES6中的静态成员
这篇关于将一个静态函数调用到React ES6类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文