Basically I've been making a web app and been trying to figure out a way to be able to move the data from an api on a dropdown list and make it where when the user clicks on the item they want to "save" to the localstorage, but for some reason when you click on it the first time it doesn't register and you have to click on it a second time and then I tried to get it to save in localstorage and it just gives me [object Object]
const [count, setCount] = useState(0);
const [products, setProducts] = useState([]);
const [product, setProduct] = useState([]);
const setProductData = (data, i) => {
setCount(count + 1);
let product = {
id: data.id,
product_number: data.product_number
}
setProduct([...product, { data }]);
localStorage.setItem(`Product${i + 1}`, [product.id, product.product_number])
console.log(data);
}
const fetchProducts = async () => {
await Axios.get(`${process.env.REACT_APP_API}/products`, {
withCredentials: true
})
.then(res => {
let data = res.data;
setProducts(data.data);
}).catch(error => {
console.error('Error:', error);
});
}
useEffect(() => {
fetchProducts();
}, [count]);
return (
<div>
<h1>Select products</h1>
<div className="mt-5 mb-5">
<InputGroup>
<DropdownButton
variant="outline-secondary"
title={'Products'}
id="dropdown"
>
{
products.map((obj, i) => {
let product = obj.product_number;
return (
<Dropdown.Item
key={i}
onClick={() => setProductData(obj, i)}>{product}</Dropdown.Item>
)
})
}
</DropdownButton>
</InputGroup>
</div>
</div>
)
I've tried multiple ways to make the localStorage.setItem() to try and get the whole object but either shows as [object Object] or only part of what I am needing
and with the project object in the setProductData function, I already tried using the data argument and that just gives the [object Object]