Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map data in reactJS?

When I write items.map it gives me items.map is not a function. I am doing it for wishlist, taking data from localStorage, but I can’t map it.

how can i map this data for write x.id for example

items?.map((x)=>{console.log(x.id)})

is not working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React, { useEffect, useState } from 'react';

const Wishlist = () => {
  useEffect(() => {
    const items = localStorage.getItem('liked');
    items?.map((x) => {
      console.log(x.id);
    });

    console.log('items', items);
  });

  return <div className="test">hello world</div>;
};

export default Wishlist;

console.log('items', items); is working. I can see [{ my all data},{like this},{there everything good}]

>Solution :

The Returned data from local storage is mostly string (or whatever it is, it’s not an array) so logically that you cannot use array methods (not just map)

So to solve this problem you need to parse the data and by this, you can iterate over it.

a code that demonstrates how to parse it

const parsedData = JSON.parse(localStorage.getItem("liked"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading