I need to access data from AsycStorage to pass to one of my functions, WidgetGenerator, immediately when the app is opened. However, there doesn't seem to be a way to call getData() and wait for it to set the value of initVals, it just continues executing code sequentially in WidgetGenerator() and passes undefined to the component it returns because getData() hasn't returned yet. Also, it doesn't seem like react likes it when I make WidgetGenerator an async function. What do I do so that I will be able to access AsyncData on loading?
const getData = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@1629349495937')
if (jsonValue != null) {
//console.log(JSON.parse(jsonValue));
initVals = JSON.parse(jsonValue);
}
} catch(e) {
// error reading value
}
}
function WidgetGenerator(props) {
switch (props.args[0]) {
case "heading":
return <UIComponents.UIHeading
text={props.args[1]}
id={props.id}
init={initVals ? initVals[props.id] : undefined}
/>;
case "checkbox":
return <UIComponents.UICheckbox
text={props.args[1]}
id={props.id}
callback={props.callback}
init={initVals ? initVals[props.id] : undefined}
/>;
}
return <View />;
}
return (
<View>
{widgetArray.map((data, index) => <WidgetGenerator key={index} id={index} args={data} callback={collectData}/>)}
<UIComponents.UISubmitButton callback={submitData} />
</View>
);
}