我的代码在 Android 上运行完美,但在 iOS 上显示错误。
iOS 错误:
我无法理解这个错误;它与 AsyncStorage
相关吗?
为什么在 iOS 设备上会发生这种情况?
第一个文件
我的导入
import React, {Component} from 'react';
import { Alert, Dimensions, Image, TouchableOpacity, AsyncStorage } from 'react-native';
import { Container, Body, Footer, Header, Input, Item, Left, Text, Title, Right, View, Button, Label, Form} from 'native-base';
import { SimpleLineIcons, Ionicons } from '@expo/vector-icons';
import { NavigationActions } from 'react-navigation';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { LinearGradient } from 'expo';
import { StatusBar } from "react-native";
import { Grid, Row, Col } from 'react-native-easy-grid';
import Toast, {DURATION} from 'react-native-easy-toast';
import Strings from '../utils/Strings';
var width = Dimensions.get('window').width;
export default class Login extends Component {
static navigationOptions = {
header: null
};
constructor() {
super();
this.state = {
MobileNo: '',
};
}
login = () => {
AsyncStorage.setItem('mobileno', MobileNo);
const { MobileNo } = this.state;
console.log("Expected login number " + MobileNo);
fetch('http://demo.weybee.in/Backend/controller/User_Login.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
mobileno: MobileNo
})
}).then((response) => response.json())
.then((responseJson) => {
// If server response message same as Data Matched
if(responseJson != 'Enter valid phone number') {
const { navigation } = this.props;
// Then open Profile activity and send user email to profile activity.
this.props.navigation.navigate('ForgetPass');
} else {
this.refs.toast.show('Invalid Number', DURATION.LENGTH_LONG);
}
}).catch((error) => {
console.error(error);
});
}
}
第二个文件
我的导入
import React, {Component} from 'react';
import { Alert, Dimensions, Image, TouchableOpacity, AsyncStorage } from 'react-native';
import { Container, Body, Footer, Header, Input, Item, Left, Text, Title, Right, View, Button, Label, Form} from 'native-base';
import { SimpleLineIcons, Ionicons } from '@expo/vector-icons';
import { NavigationActions } from 'react-navigation';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { LinearGradient } from 'expo';
import { StatusBar } from "react-native";
import { Grid, Row, Col } from 'react-native-easy-grid';
import Toast, {DURATION} from 'react-native-easy-toast'
import Strings from '../utils/Strings';
import OtpInputs from 'react-native-otp-inputs';
var width = Dimensions.get('window').width;
export default class Login extends Component {
static navigationOptions = {
header: null
};
constructor() {
super();
this.state = {
MobileNo: '',
mobileNumber: '',
code: '',
}
}
componentDidMount() {
AsyncStorage.getItem('mobileno').then((mobileNo) => {
if(mobileNo){
this.setState({ mobileNumber: mobileNo });
}
});
}
PTP = () => {
let mobileNumber = JSON.parse(this.state.mobileNumber);
console.log("login number " + mobileNumber);
let {code} = this.state;
console.log(code);
fetch('http://demo.weybee.in/Backend/controller/Get_PTP.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
mobileno: mobileNumber,
code: code,
})
}).then((response) => response.json())
.then((responseJson) => {
// If server response message same as Data Matched
if(responseJson != 'Enter valid phone number') {
const { navigation } = this.props;
// Then open Profile activity and send user email to profile activity.
this.props.navigation.navigate('Home');
} else {
this.refs.toast.show('Invalid PTP', DURATION.LENGTH_LONG);
}
}).catch((error) => {
console.error(error);
});
}
}
最佳答案
我认为问题可能在于您如何将 MobileNo
保存到 AsyncStorage。 MobileNo
不是状态的一部分吗?它不应该被称为 this.state.MobileNo
吗?
在 FirstFile 里面,这就是问题所在,
AsyncStorage.setItem('mobileno', MobileNo);
应该是,
AsyncStorage.setItem('mobileno', this.state.MobileNo);
关于ios - 为什么我在 React Native iOS 中收到“无法识别的选择器发送到实例”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56913807/