This looks like it is just going to overwrite myItems
with whatever the most recent mapped item
is rather than appending each new item.
You might try mapping the items to an array in componentDidMount()
and then set the state in a useEffect
call.
const BasicScreen = () => {
const [myData, setData] = useState([]);
const [myItems, setItems] = useState([]);
const checkForItems = () => {
var storageItems = AsyncStorage.getItem("MyItems").then((item) => {
if (item) {
return JSON.parse(item);
}
});
setItems(storageItems);
};
useEffect(() => {
async function getItems() {
await checkForItems();
}
getItems();
}, []);
return (
<View>
<Text>{myItems[0]}</Text>
</View>
);
};
export default BasicScreen;
CLICK HERE to find out more related problems solutions.