I'm building my website with React, and I'm using useState to get the id of hovered item.
But when I use setState to get the value, it doesn't update and remain 'null'
So on console, I can see the event.target.id is recognized, but even after the setState event, the state value return null.
Can anybody give some clues on what is happening here?
Here's the code
import React from 'react'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import styled from 'styled-components'
const WorksTitle = props => {
const { type, title, imgSrc, link, year } = props
const [currentMenu, setCurrentMenu] = React.useState(null)
const handleHover = event => {
console.log(
'event target: ',
event.target,
' / ',
'event target id: ',
event.target.id
)
**console.log(event.target.id)
setCurrentMenu(event.target.id)
console.log(currentMenu)**
}
const handleMouseOut = event => {
console.log('mouse out from:', event.target.id)
setCurrentMenu(null)
}
return (
<StyledWorksTitle>
<TitleItem id={title} onMouseOverCapture={handleHover} onMouseOut={handleMouseOut}>
<div className={`itemWrapper ${(currentMenu === { title } ||
currentMenu === null) && 'activated'}`}
id={title}>
<Link to={link}></Link>
</div>
</TitleItem>
</StyledWorksTitle>
)
}
/* styled-components */
export default WorksTitle