There is another approach that uses a reusable rendering component that will make the components βhangβ or βopenβ - whatever makes sense.
class Hoverable extends Component { constructor() { super(); this.state = { isMouseInside: false }; } mouseEnter = () => { this.setState({ isMouseInside: true }); } mouseLeave = () => { this.setState({ isMouseInside: false }); } render() { return this.props.children( this.state.isMouseInside, this.mouseEnter, this.mouseLeave ) } }
Then create a functional component that represents an element that may be overhanging. For example album
const HoverableElement = props => ( <Hoverable> {(isMouseInside, mouseEnter, mouseLeave) => ( <div className="menu-item"> <div onMouseEnter={mouseEnter} onMouseLeave={mouseLeave}> <h2>{props.title}</h2> </div> {isMouseInside && props.children} </div> )} </Hoverable> )
Finally, use a HoverableElement to display a list of items, each of which will be "hoverable", such as an array of albums
class HoverableElementsList extends Component { render() { return ( <div> <HoverableElement title="First Menu"> <p>Some children content</p> </HoverableElement> </div> ) } }
cjjenkinson
source share