I'm building this code in when a user clicks the image, it should assign the newstyle to the style state. But whenever I click in a position of the image, it pops up an newStyle is undefined error even when I've set the variable newStyle inside the imageClick function.
import React, {useState} from 'react'
import mainImage from '../img/marioBrosMain.jpg'
import '../main.css'
import Box from '../pages/box.js'
function Main() {
const [style, setStyle] = useState();
const imageClick = (x, y) => {
const newStyle = `position: absolute,
z-index: 2,
border-radius: 2px solid red,
border: 2px solid red,
height: 50px,
padding-left: 50px,
margin-left: 1450px,
left:${x}px,
top:${y}px`
return newStyle
}
return (
<div id="frame">
<div id="mainImage" style = {style}></div>
<img src={mainImage} className="marioBros____image" onClick={e => {
imageClick(e.screenX, e.screenY);
setStyle(newStyle);
}}/>
</div>
)
}
export default Main