在 AsyncStorage 中设置项目时,未定义不是对象
Undefined is not an object when setting item in AsyncStorage
在我的 React Native 应用程序中,我想将令牌存储到 AsyncStorage
中,但是当尝试这样做时,它会抛出以下 warning.I 经历了许多关于此类问题的 SO 答案但是想不出解决办法。
SignupScreen.js
import React from "react";
import { View } from "react-native";
import { AsyncStorage } from '@react-native-community/async-storage'
import PhoneInput from "react-native-phone-input";
import {
Button,
Text,
Form,
Item as FormItem,
Input,
Label,
} from 'native-base';
export default class Signup extends React.Component {
static navigationOptions = {
drawerLabel: "Signup",
};
constructor(props) {
super(props);
this.state = {
fname: "",
mobile: "",
};
}
setToken = async () => {
//This is where the warning is throws
await AsyncStorage.setItem('token', 'tokka').then(
val => {
if(val) this.props.navigation.navigate('Dashboard')
}
)
}
render() {
return (
<View style={{paddingTop: "40%"}}>
<Text style={{textAlign: "center",fontSize: 40}}>OnTask</Text>
<Text style={{fontSize: 20,textAlign: "center"}}>Signup</Text>
<Form>
<FormItem>
<Label>First Name</Label>
<Input />
</FormItem>
<Label style={{marginTop: "3%",marginLeft: "4%"}}>Mobile Number</Label>
<PhoneInput
ref="phone"
style={{
height: 50,
padding: 10,
width: 300,
marginLeft: "2%",
marginBottom: "5%",
borderRadius: 10
}}
onChangePhoneNumber={ number => this.setState({mobile: number})}
/>
<Button full primary onPress={() => this.setToken()}>
<Text> Sign Up </Text>
</Button>
</Form>
</View>
);
}
}
问题是您错误地导入了 AsyncStorage。请导入不带花括号的 AsyncStorage。
import AsyncStorage from '@react-native-community/async-storage';
而不是
import { AsyncStorage } from '@react-native-community/async-storage';
最好使用 try and catch
setToken = async () => {
try {
const val = await AsyncStorage.setItem('token', 'tokka');
if(val) this.props.navigation.navigate('Dashboard')
} catch (e) {
console.log('error', e.message);
}
}
在我的 React Native 应用程序中,我想将令牌存储到 AsyncStorage
中,但是当尝试这样做时,它会抛出以下 warning.I 经历了许多关于此类问题的 SO 答案但是想不出解决办法。
SignupScreen.js
import React from "react";
import { View } from "react-native";
import { AsyncStorage } from '@react-native-community/async-storage'
import PhoneInput from "react-native-phone-input";
import {
Button,
Text,
Form,
Item as FormItem,
Input,
Label,
} from 'native-base';
export default class Signup extends React.Component {
static navigationOptions = {
drawerLabel: "Signup",
};
constructor(props) {
super(props);
this.state = {
fname: "",
mobile: "",
};
}
setToken = async () => {
//This is where the warning is throws
await AsyncStorage.setItem('token', 'tokka').then(
val => {
if(val) this.props.navigation.navigate('Dashboard')
}
)
}
render() {
return (
<View style={{paddingTop: "40%"}}>
<Text style={{textAlign: "center",fontSize: 40}}>OnTask</Text>
<Text style={{fontSize: 20,textAlign: "center"}}>Signup</Text>
<Form>
<FormItem>
<Label>First Name</Label>
<Input />
</FormItem>
<Label style={{marginTop: "3%",marginLeft: "4%"}}>Mobile Number</Label>
<PhoneInput
ref="phone"
style={{
height: 50,
padding: 10,
width: 300,
marginLeft: "2%",
marginBottom: "5%",
borderRadius: 10
}}
onChangePhoneNumber={ number => this.setState({mobile: number})}
/>
<Button full primary onPress={() => this.setToken()}>
<Text> Sign Up </Text>
</Button>
</Form>
</View>
);
}
}
问题是您错误地导入了 AsyncStorage。请导入不带花括号的 AsyncStorage。
import AsyncStorage from '@react-native-community/async-storage';
而不是
import { AsyncStorage } from '@react-native-community/async-storage';
最好使用 try and catch
setToken = async () => {
try {
const val = await AsyncStorage.setItem('token', 'tokka');
if(val) this.props.navigation.navigate('Dashboard')
} catch (e) {
console.log('error', e.message);
}
}