I'm feeling lost and frustrated after trying numerous solutions. I can't seem to figure out why I keep getting this error while attempting to read from asyncstorage in React Native. I know that in order to retrieve asyncstorage as a string, I need to parse it, but I'm continuously encountering this particular type of issue.
class AddScreen extends React.Component {
constructor(props) {
i = 0;
super(props);
var text = {};
this.state = {text: ''};
}
setName = (text) => {
text.name = text;
var name = ("name"+i++);
AsyncStorage.setItem(name, text);
this.setState({name: text});
var booga= AsyncStorage.getItem(name);
var booga1 = JSON.parse(booga);
alert(booga1);
}
render() {
return (
<View style={{flex: 1, alignItems: "center", justifyContent: "center"}}>
<TextInput
style={{height: 100}}
placeholder="Name of expense"
onChangeText={this.setName}
/>
<View style={{width: 50, height: 100}} />
<Text>Add Screen</Text>
</View>
);
}
}
When running the app on my device using Expo, I encounter the following error:
Unexpected token o in JSON at position 1
Object.AddScreen._this.setName [as onChangeText]
App.js:26:20
Object._onChange
TextInput.js:1252:42
Object.invokeGuardedCallbackImpl
ReactNativeRenderer-dev.js:93:9
invokeGuardedCallback
ReactNativeRenderer-dev.js:318:30
invokeGuardedCallbackAndCatchFirstError
ReactNativeRenderer-dev.js:342:24
executeDispatch
ReactNativeRenderer-dev.js:715:2
executeDispatchesInOrder
ReactNativeRenderer-dev.js:737:4
executeDispatchesAndRelease
ReactNativeRenderer-dev.js:894:4
executeDispatchesAndReleaseTopLevel
ReactNativeRenderer-dev.js:902:9