async-storage: AsyncStorage not working properly
Current behavior
AsyncStorage not working properly on 0.60.5 I set and get the async storage with the help of two button. Value successfully sets and gets but when i reload the application and tried to get the value again without setting it return null.
Expected behavior
Async save the set value before and don’t remove it until we manually set it to null. But now every time application is reloaded it forgets the saved async value and i have to set it again and again. Basically setup my authentication with asyncstorage, if user is signed in it shows dashboard else login screen. But now user login and open app again, it shows login screen again and again. It was working fine on Android 8.1 with React Native 0.59. But i upgraded my project to 0.60, its behavior changed.
Code
export default class LibTest extends Component {
static navigationOption = {
title: "LibTest", header: null, headerVisible: false
};
constructor(props) {
super(props);
this.state = {
asyncVal: null,
storedData: "myValue"
}
}
async componentDidMount() {
}
storeData = async () => {
console.log("inside storeData")
try {
await AsyncStorage.setItem('Test', 'TestValue')
} catch (e) {
console.log(e)
}
}
getData = async () => {
console.log("inside getData")
try {
const value = await AsyncStorage.getItem('Test')
this.setState({ storedData: value })
} catch (e) {
// error reading value
}
}
render() {
return(
<View style={{ marginTop: 40 }}>
<Text> {this.state.storedData}</Text>
<Button title={"storeData"} onPress={this.storeData}><Text>storeData</Text></Button>
<Button title={"getData"} onPress={this.getData}><Text>getData</Text></Button>
</View>
)
}
}
Environment
- Async Storage version: 1.6.1
- React-Native version: 0.60.5
- Platform tested: Android 9
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 10
- Comments: 32 (9 by maintainers)
Setting values to
null
/undefined
is not supported - probably the error is thrown there, which you don’t handle. If you want to remove value, use.removeItem
method@Krizzu I found bug in my code, thanks for your help, and sorry for trouble. The problem was that I checked the state in one of my Context which was not yet updated with the value stored in AsyncStorage while first rendered.
i have same issue… when i set any value to null, its ok but after a refresh everything rolls back…
“react-native”: “0.61.2”, “@react-native-community/async-storage”: “^1.6.1”,
I’m having the same issue too. Async Storage version: 1.6.2 React-Native version: 0.60.5