user2194838 user2194838 - 1 year ago 204
React JSX Question

onMouseLeave event is not working

This is nav component. This nav component rendered at run time. Text should be visible when mouse will enter to div with ref "text_nav" and text should hide when mouse will leave div with ref "text_nav".

onMouseLeave is not working

var React = require('React');
var $ = require('jquery');

var Nav = React.createClass({
getInitialState: function() {
return {
items: []
componentWillMount: function() {
var _this = this;
this.serverRequest =
$.post("/nav", {}, function(result) {

onMouseEnter: function() { = {display: true}
onMouseLeave: function() { = {display: 'none'}
render: function() {
var text =, index) {
var icon = "text_" + data.sname;
return (
<div id={icon} key={index} className="text_nav_item">
<span><a href={data.url}>{data.title} </a></span>

return (
<div id="nav" className="fixed" style={{zIndex: 1018}} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
<div id="text_nav" ref="text_navigator" style={{display: 'none'}} >
<div id="text_nav_content">

Answer Source

First off, the ref is text_nav in your render method, not text_navigator as you're using in onMouseEnter and onMouseLeave. But the main issue is that you can't set the display style the way you're trying to do, as = {display: true}

The most common approach to take is to set a boolean in state, perhaps called displayTextNavigator. In getInitialState, set it to false, and then your onMouseEnter and onMouseLeave functions can be:

  onMouseEnter: function() {
        this.setState({ displayTextNavigator: true})
   onMouseLeave: function() {
        this.setState({ displayTextNavigator: false})

Now in your render method, you can change your wrapping div to look like this:

<div id="text_nav" ref="text_nav" style={{display: this.state.displayTextNavigator ? 'block': 'none'}} > You can pull that ternary operator out to earlier in the render method if you'd like it be more readable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download