Why am I getting prevState value in localStorage, I know they are asynchronous, but I am not able to understand properly, which concept I need to know?

const addProduct = (passedProduct, index) => {

let exists = products.find((val) => val.id == passedProduct.id);

if (!exists) {

const count1 = count + 1;

setCount(count1);

localStorage.setItem(“count”, count1);

setProduct([…products, { …passedProduct, quantity: 1 }]);

console.log(products);

} else {

setProduct(

products.map((val) =>

val.id === exists.id

? { …exists, quantity: exists.quantity + 1 }

: val,

),

);

}

localStorage.setItem(“products”, JSON.stringify(products));

};

Welcome to the forum @ikram12,

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Happy coding