React native 如何修复';未定义不是对象';使用OnPress时
我在抽屉导航的屏幕上添加了一个呼叫按钮。 然而,即使我已经绑定了“this”,以便保留其上下文,当我按下Call按钮时,我收到一个“undefined is not a object”(评估_this6.App.dialCall)。 以下是相关代码:React native 如何修复';未定义不是对象';使用OnPress时,react-native,React Native,我在抽屉导航的屏幕上添加了一个呼叫按钮。 然而,即使我已经绑定了“this”,以便保留其上下文,当我按下Call按钮时,我收到一个“undefined is not a object”(评估_this6.App.dialCall)。 以下是相关代码: render() { return ( <View style={{ flex: 1, backgroundColor: "black", alignItems: 'center', justif
render() {
return (
<View style={{ flex: 1, backgroundColor: "black", alignItems:
'center', justifyContent: 'center' }}>
<Text style={{backgroundColor: "black", fontSize: 16, color:
"white"}}>Dour services are available 24 hours a day, so you can
truly rely on us.
We will match or try and beat any price.
</Text>
<Button title='Call' onPress={() =>
this.App.dialCall.bind(this)}
/>
</View>
);
render(){
报税表(
Dour服务每天24小时提供,因此您可以
真正依靠我们。
我们将匹配或尝试击败任何价格。
this.App.dialCall.bind(this)}
/>
);
(这在抽屉导航的主屏幕上。
我的函数在主类中:
export default class App extends Component {
dialCall = () => {
let phoneNumber = '';
if (Platform.OS === 'android') {
phoneNumber = 'tel:${1234567890}';
}
else {
phoneNumber = 'telprompt:${1234567890}';
}
Linking.openURL(phoneNumber);
};
render() {
return <AppContainer />;
}
}
导出默认类应用程序扩展组件{
拨号呼叫=()=>{
让phoneNumber='';
如果(Platform.OS==='android'){
电话号码='电话:${1234567890}';
}
否则{
电话号码='telprompt:${1234567890}';
}
Linking.openURL(电话号码);
};
render(){
返回;
}
}
如果您使用的是来自不同类的函数/方法,那么您必须调用该子类,比如说父类中的类A,或者说它的构造函数中的类B作为对象
constructor(props) {
super(props);
this.objectOfChildClass = new App();
this.state = {
};
}
完成此操作后,使用this.objectOfChildClass.functionName
另外,当您已经在使用fat arrow函数时,您不需要绑定该函数。它的功能与bind相同
如果您使用的是来自不同类的函数/方法,那么您必须调用该子类,比如说父类中的类A,或者说它的构造函数中的类B作为对象
constructor(props) {
super(props);
this.objectOfChildClass = new App();
this.state = {
};
}
完成此操作后,使用this.objectOfChildClass.functionName
另外,当您已经在使用fat arrow函数时,您不需要绑定该函数。它的功能与bind相同
实际上,我是这样做的,如下所示,但它提出了“类型错误-未定义不是对象,评估_this%.dialcall.bindclass DashboardScreen扩展组件{static navigationOptions={title:'Home',};构造函数(props){super(props);this.dialCall=this.dialCall.bind(this);}另外,当您已经在使用fat arrow函数时,您不需要绑定该函数。它执行与bind相同的操作。this.dialCall}
class DashboardScreen扩展组件{static navigationOptions={title:'Home',};构造函数(props){super(props);this.objectOfChildClass=new App();}
实际上,我是这样做的,如下所示,但它提出了“类型错误-未定义的不是对象,评估_this%.dialcall.bindclass DashboardScreen扩展组件{static navigationOptions={title:'Home',};构造函数(props){super(props);this.dialCall=this.dialCall.bind(this);}另外,当您已经在使用fat-arrow函数时,不需要绑定该函数。它与bind的作用相同。this.dialCall}class DashboardScreen扩展组件{static navigationOptions={title:'Home',};构造函数(props){super(props);this.objectOfChildClass=newapp();}