react native cannot update a component from within the function body of another component

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.

Leave a Comment

Your email address will not be published.

Scroll to Top