我在狀態這些屬性我的組件類型錯誤:無法讀取屬性「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);
}
你可以嘗試控制檯登錄this.state.receiptnumber渲染方法後立即解釋一下嗎? –
如果它仍然未定義:嘗試這樣做 添加componentWillMount方法,並把它放在這裏面 this.setState({receiptnumber:'your number' }); –
@GaneshCauda是的。即使在'_onPress'中也可以訪問。你可以請檢查更新的問題。它不工作'Promise.resolve',但我需要'Promise.resolve'權限 –