在事件处理程序中调用方法未定义
Invoking method inside event handler is undefined
以下示例中,当不需要任何参数时,handleButtonPress
函数正常工作…
import React, { Component } from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
export default class MyComponent extends Component {
constructor(props){
super(props)
this.state = {message:"HELLO"}
this.myFunc = this.myFunc.bind(this)
this.handleButtonPress = this.handleButtonPress.bind(this)
}
render(){
return (
<View>
<Text>{this.state.message}</Text>
<TouchableOpacity onPress={this.handleButtonPress}>
<Text>Press Me</Text>
</TouchableOpacity>
</View>
)
}
handleButtonPress(){
console.log("BUTTON WAS PRESSED")
this.myFunc()
}
myFunc(){
console.log("MY FUNCTION WAS CALLED")
this.setState({message:"GOODBYE"})
}
}
,但在需要参数时不起作用:
render(){
return (
<View>
<Text>{this.state.message}</Text>
<TouchableOpacity onPress={function(){ this.handleButtonPress("GOODBYE") }}>
<Text>Press Me</Text>
</TouchableOpacity>
</View>
)
}
handleButtonPress(message){
console.log("BUTTON WAS PRESSED WITH MESSAGE: " + message)
this.myFunc(message)
}
myFunc(message){
console.log("MY FUNCTION WAS CALLED")
this.setState({message:message})
}
抛出:undefined is not a function (evaluating 'this.handleButtonPress("GOODBYE")')
我一直在使用的一个策略是在render
函数中再次引用handleButtonPress
函数,如下所示:
render(){
handlePress = this.handleButtonPress;
return (
<View>
<Text>{this.state.message}</Text>
<TouchableOpacity onPress={function(){ handlePress("GOODBYE") }}>
<Text>Press Me</Text>
</TouchableOpacity>
</View>
)
}
但是有其他/更好的方法吗?
因为有一个匿名函数,所以里面的this
上下文是全局window
对象。由于没有handleButtonPress
存在,它抛出undefined
不是函数的错误。您的解决方法有效,因为this
仍然引用匿名函数之外的类,因此允许您将其引用分配给handlePress
并调用它。
为了解决这个问题,可以使用Function.prototype.bind
,它将为函数补充this
上下文。从文档链接:
bind()
方法创建了一个新函数,当调用该函数时,将其this
关键字设置为提供的值…
你可以在这里像这样应用:
<TouchableOpacity onPress={function() { this.handleButtonPress("GOODBYE") }.bind(this)}>
这将把匿名函数的this
上下文设置为类,而不是全局window
对象,从而允许您调用this.handleButtonPress
。然后可以按照文档中提到的再次压缩上述内容:
bind()
方法创建一个新函数,当调用该函数时,将其this关键字设置为所提供的值,在调用新函数时所提供的任何参数之前具有给定的参数序列。(强调我的)语法
fun.bind(thisArg[, arg1[, arg2[, ...]]])
其中arg1, arg2
等是bind
的可选参数,可以绑定到函数。可以这样应用:
<TouchableOpacity onPress={this.handleButtonPress.bind(this, "GOODBYE")}>
这完全摆脱了匿名函数,但是this
仍然必须在bind
中传递,因为您在handleButtonPress
方法中使用它。
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 使用javascript ajax post方法的未定义偏移PHP错误
- jQuery验证器添加方法未定义
- 定义的静态方法未定义
- jQuery Validate() 和 Valid() 方法未定义或不起作用
- 为什么内部函数中当前对象的方法未定义
- 如何解决“不能调用方法...未定义“错误
- 得到"无法调用方法..未定义的“;JavaScript和Mustache出现错误
- JavaScript方法未定义
- initialize()方法未定义
- ExpressJS PUT方法未定义的对象问题
- JavaScript - Prototype方法未定义
- 映射内匿名函数内的方法未定义
- 在事件处理程序中调用方法未定义
- 在setTimeOut函数中传递多个参数会抛出Uncaught ReferenceError:方法未定义(匿名函数)
- Javascript对象方法未定义
- app()方法未定义
- 未捕获的类型错误:不能调用方法'未定义使用Ckeditor
- QuickTime对象的SetURL方法未定义