简体   繁体   中英

onmouseover not working with React.js

The click event works fine, but the onmouseover event does not work.

ProfImage = React.createClass({

    getInitialState: function() {
        return { showIcons: false };
    },

    onClick: function() {

        if(this.state.showIcons == true) {
            this.setState({ showIcons: false });
        }
        else {
            this.setState({ showIcons: true });
        }
    },

    onHover: function() {
        this.setState({ showIcons: true });
    },

    render: function() {

        return (
            <div>
            <span className="major">
                <img src="/images/profile-pic.png" height="100" onClick={this.onClick} onmouseover={this.onHover} />
            </span>


            { this.state.showIcons ? <SocialIcons /> : null }
            </div>

        );
    }

});

你需要把一些字母大写。

<img src="/images/profile-pic.png" height="100" onClick={this.onClick} onMouseOver={this.onHover} />

上面的答案都是正确的,但你需要将这些方法绑定到类上下文!

<img src="/images/profile-pic.png" height="100" onClick={this.onClick.bind(this)} onMouseOver={this.onHover.bind(this)} />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM