cazorna 77
cazorna 77

Reputation: 43

How to fix error (cant access lexical declaration Array before initialization ) in react.js?

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];
    })

Upvotes: 1

Views: 698

Answers (2)

xijdk
xijdk

Reputation: 500

Simply change your code to const arr = Array.from(new Set(array)). Array is js inner variable.

Upvotes: 0

cazorna 77
cazorna 77

Reputation: 43

It workin when I change

const Array = Array.from(new Set(array));

to

var arr =0; arr = Array.from(new Set(array));

you cant use Array like variable because it already defined as existe function in javascript(React.js)

Upvotes: 1

Related Questions