我有以下ReactJS类:
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
我不明白 这是调用静态函数的好方法吗?我认为react正在使用静态进行某些操作,但我不知道该怎么做。
需要在类而非实例上访问静态方法。因此,在您的情况下,请使用:
Content.getValue()
但是,静态方法将无法访问this-基于上面的代码示例,我不认为您希望该方法是静态的。
this
更多:ES6中的静态成员