LHI*_*OUI 5 javascript ecmascript-6 reactjs react-native
我有一个像这样定义的组件
export class A extends Component{
constructor(props){
this.state = {
scene:0
}
}
static changeScene(scene){
this.setState({scene:scene})
}
}
Run Code Online (Sandbox Code Playgroud)
我想从任何地方调用更改场景A.changeScene(sceneVal)
来更改 A 中的场景。问题是我无法访问 this.setState 我收到此错误Unhandled JS Exception: this.setState is not a function.
我确定 A 组件已经安装。我可以通过var self = null;在构造函数self = this中定义全局变量和构造函数内部 来绕过这个错误,但我想要一个更好的方法来解决这个问题
原因是,如果您使用static function方法,static则将无法访问this该函数内部。您应该避免使用static函数。方法无法访问在usingStatic的实例上定义的值、属性和方法。classthis
检查这篇文章:http://odetocode.com/blogs/scott/archive/2015/02/02/static-members-in-es6.aspx
| 归档时间: |
|
| 查看次数: |
6324 次 |
| 最近记录: |