Whenever I try to use items.map
, it returns an error saying items.map is not a function. I am working on my wishlist feature, retrieving data from localStorage
, but I can't seem to perform the map
operation successfully.
How do I iterate over this data in order to access x.id
for example?
items?.map((x) => {console.log(x.id)})
The above code snippet doesn't seem to be functioning as intended.
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;
The line console.log('items', items);
does work. It displays
[{ my all data},{like this},{there everything good}]