类型错误:无法读取属性“receiptnumber”的未定义的反应本地
问题描述:
我在状态这些属性我的组件类型错误:无法读取属性“receiptnumber”的未定义的反应本地
constructor(props) {
super(props);
this._onPress = this._onPress.bind(this);
this.state = {
receiptnumber: "",
text: "",
time: "",
isDateTimePickerVisible: false
};
}
我已绑定在其上运行得很好按钮的单击事件_onPress
方法。我打电话给另一种方法_saveScheduleMessage
内_onPress
方法,我试图访问this.state.receiptnumber
我收到一个错误。
_onPress() {
try {
if (Platform.OS === "android") {
if (Platform.Version >= 23) {
Promise.resolve(requestSendSMSPermission()).then(function(result) {
// user granted SMS permission
if (result) {
this._saveScheduleMessage;
} else {
// user denied SMS permission
Alert.alert(
strings.permission_required,
strings.send_sms_deny_permission
);
}
});
}
}
} catch (error) {
console.log(error);
}
}
_saveScheduleMessage() {
realm.write(() => {
realm.create("NewMessage", {
receiptNumber: this.state.receiptnumber,
text: this.state.text,
time: this.state.time
});
});
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: "DashboardScreen"
})
]
});
navigate.dispatch(resetAction);
}
PermissionManager.js
import { PermissionsAndroid } from "react-native";
module.exports = {
requestSendSMSPermission: async function() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.SEND_SMS
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
} catch (err) {
console.warn(err);
}
}
};
这是我特意打电话_onPress
方法
<RoundButton
textStyle={styles.roundTextStyle}
buttonStyle={styles.roundButtonStyle}
onPress={() => this._onPress()}
>
错误日志:
D:\React Native\application\node_modules\react-native\Libraries\ReactNative\YellowBox.js:76 Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'receiptnumber' of undefined
TypeError: Cannot read property 'receiptnumber' of undefined
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:106446:60
at tryCallOne (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:16011:12)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:16097:15
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7370:19
at _callTimer (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7284:7)
at Object.callImmediatesPass (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7512:9)
at Object.callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7523:21)
at MessageQueue.__callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6882:16)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6768:16
at MessageQueue.__guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6868:9)
console.warn @ D:\React Native\application\node_modules\react-native\Libraries\ReactNative\YellowBox.js:76
onUnhandled @ D:\React Native\application\node_modules\react-native\Libraries\Promise.js:35
onUnhandled @ D:\React Native\application\node_modules\promise\setimmediate\rejection-tracking.js:71
(anonymous) @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:223
_callTimer @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:143
callTimers @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:372
__callFunction @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:266
(anonymous) @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:103
__guard @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:231
callFunctionReturnFlushedQueue @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:102
(anonymous) @ debuggerWorker.js:72
有谁知道我可以修复它?
它工作时,我删除Promise.resolve
但我需要它的权限的东西。
_onPress() {
realm.write(() => {
realm.create("NewMessage", {
receiptNumber: this.state.receiptnumber,
text: this.state.text,
time: this.state.time
});
});
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: "DashboardScreen"
})
]
});
this.props.navigation.dispatch(resetAction);
}
答
尝试更换
function(result)
到
result =>
我真的不能解释的理论,但基本上这个 '=>' 语法是抱着你的函数不会立即执行,
例如,如果你做这样的事情
onPress = {this.onPress}
是this.onPress方法会立即执行,即使没有新闻发布会
也许一些专家能够更恰当
+0
你可以写为什么它不工作没有=> –
+0
完成先生,也许我没有适当的解释,但那我现在知道这件事 –
你可以尝试控制台登录this.state.receiptnumber渲染方法后立即解释一下吗? –
如果它仍然未定义:尝试这样做 添加componentWillMount方法,并把它放在这里面 this.setState({receiptnumber:'your number' }); –
@GaneshCauda是的。即使在'_onPress'中也可以访问。你可以请检查更新的问题。它不工作'Promise.resolve',但我需要'Promise.resolve'权限 –