事件处理程序中的调用方法未定义
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>
)
}
但是有another/better方法吗?
因为有匿名函数,里面的this
上下文就是全局window
对象。由于不存在 handleButtonPress
,因此会抛出 undefined
不是函数的错误。您的解决方法有效,因为 this
仍然引用匿名函数外部的 class,因此您可以将它的引用分配给 handlePress
并调用它。
为了解决这个问题,您可以使用 Function.prototype.bind
来补充函数的 this
上下文。来自链接的文档:
The bind()
method creates a new function that, when called, has its this
keyword set to the provided value...
你可以在这里应用它:
<TouchableOpacity onPress={function() { this.handleButtonPress("GOODBYE") }.bind(this)}>
这会将匿名函数的 this
上下文设置为 class,而不是全局 window
对象,从而允许您调用 this.handleButtonPress
。然后可以按照文档所述再次压缩上述内容:
The bind()
method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. (emphasis mine)
Syntax
fun.bind(thisArg[, arg1[, arg2[, ...]]])
其中 arg1, arg2
等是可以绑定到函数的 bind
的可选参数。这可以像这样应用:
<TouchableOpacity onPress={this.handleButtonPress.bind(this, "GOODBYE")}>
这完全摆脱了匿名函数,但是 this
仍然必须在 bind
中传递,因为您在 handleButtonPress
方法中使用它。
当不需要参数时,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>
)
}
但是有another/better方法吗?
因为有匿名函数,里面的this
上下文就是全局window
对象。由于不存在 handleButtonPress
,因此会抛出 undefined
不是函数的错误。您的解决方法有效,因为 this
仍然引用匿名函数外部的 class,因此您可以将它的引用分配给 handlePress
并调用它。
为了解决这个问题,您可以使用 Function.prototype.bind
来补充函数的 this
上下文。来自链接的文档:
The
bind()
method creates a new function that, when called, has itsthis
keyword set to the provided value...
你可以在这里应用它:
<TouchableOpacity onPress={function() { this.handleButtonPress("GOODBYE") }.bind(this)}>
这会将匿名函数的 this
上下文设置为 class,而不是全局 window
对象,从而允许您调用 this.handleButtonPress
。然后可以按照文档所述再次压缩上述内容:
The
bind()
method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. (emphasis mine)Syntax
fun.bind(thisArg[, arg1[, arg2[, ...]]])
其中 arg1, arg2
等是可以绑定到函数的 bind
的可选参数。这可以像这样应用:
<TouchableOpacity onPress={this.handleButtonPress.bind(this, "GOODBYE")}>
这完全摆脱了匿名函数,但是 this
仍然必须在 bind
中传递,因为您在 handleButtonPress
方法中使用它。