I am attempting to set a randomly generated variable as the width and height of a div. I have successfully written a function that generates a random size, but encounter an error when trying to assign it to the style attribute.
My objective is to create a new div every time a button is clicked.
Here is my current progress:
style={{width:divSize}}
The error message states:
Variable 'divSize' is used before being assigned.ts(2454)
function createRandomRectangle(){
if (boxxy!=null) {
var divSize = Math.round(((Math.random()*200) + 50));
const width = boxxy.offsetWidth , height =boxxy.offsetHeight;
var posX = Math.round((Math.random() * ( width- divSize)));
var posY = Math.round((Math.random() * ( height- divSize)));
}
return(
<div className='Rectangle' style={{width:divSize}}></div>
)
}