Month: January 2024

  • Copying a state variable’s value

    To copy the value of a variable declared using useState, if it’s an array use: let varName = […stateVar]; If it’s not an array use: let varName = stateVar.valueOf();

  • Creating an array of empty arrays

    If you do: let arr = new Array(5); arr.fill([]); This will make an array of 5 identical arrays where editing one will update all which is rarely what anyone wants. To achieve an array of different arrays use: let arr = Array.from(new Array(5), () => [])