map
creates a new array whereas forEach
just loops through the array. In this case, we want createGrid
to be an array of Square
components. If you use forEach
then, createGrid
will be undefined.
According to MDN
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
whereas in forEach
The forEach() method executes a provided function once for each array element.
CLICK HERE to find out more related problems solutions.