I set the array in onchange method and I see it in the console with console.log("array =",array) with all elemets but when I want to delete the duplicate element with Array.from(new Set(array)) method in the update method , I find error in the console ( cant access lexical declaration Array before initialization ) !
const [array, setArray] = useState([]);
const update = (e) =>{
e.preventDefault(); console.log("array =",array); // = [ 1, 1, 2, 2, 51, 51 ]
if( array !==undefined){
const Array = Array.from(new Set(array)); // error } }
setarticlesolde((pre) => {
setArray((pre) => [pre.articleId]);
return [... pre];
})
arraywill always have a length of zero so:if (array.length)would be better. 2) Don't assign something toArray. Use another variable name. 3) What isartItem? 4) What issetarticlesolde, and why are you returning an array from it?const Array = Array.from(new Set(array));toconst arr = Array.from(new Set(array));and see if that fixes the problem