简体   繁体   English

React.js状态变量不会在悬停时更新

[英]React.js State Variable not updating on hover

I am using a state variable to track whether an icon has been hovered over. 我正在使用状态变量来跟踪图标是否已悬停。 The variable is not updating. 变量未更新。 Console.log of the state variable is not working either (it doesn't show in console) 状态变量的Console.log也不起作用(控制台中未显示)

Icons = React.createClass({

    getInitialState: function() {
        return { icon_id: 0};
        console.log(icon_id);
    },

    onHover: function(event) {
        this.setState({ icon_id: event.currentTarget.dataset.id });
        console.log(icon_id);
    },

    render: function() {

        return (

        <ReactCSSTransitionGroup transitionName="example" transitionAppear={true} transitionLeave={true} transitionEnterTimeout={600} transitionAppearTimeout={600} transitionLeaveTimeout={300}>


                    <ul className="someclass">{ iconslist.map(function(i){
                            return <li key={i.id}><a href={i.url} target="_blank"><span className={i.class} id={i.id} data-id={i.data} onMouseOver={this.onHover}></span></a></li>

                        }) }

                    </ul>
                    <p className="icon-text">{iconslist[this.state.icon_id].name}</p>


        </ReactCSSTransitionGroup>

        );

    }

});

var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;

var iconslist = [

    { data: '0', url: 'mailto:******', id: 'lightbulb', class: 'fa fa-lightbulb-o fa-5x', name: 'Thoughts'},
    { data: '1', url: 'mailto:******', id: 'gears', class: 'fa fa-gears fa-5x', name: 'ML'},
];

You should access any state variable as follows: 您应该按以下方式访问任何状态变量:

var iconid = this.state.icon_id;
console.log(iconid);

The setState method does not immediately update the state of the class. setState方法不会立即更新类的状态。 So the setState method also take a callback which is invoked after react has updated the state - official documentation 所以setState方法还接受了一个回调,该回调在react更新状态后被调用- 官方文档

So in your code, you'd have to do something like this: 因此,在您的代码中,您必须执行以下操作:

onHover: function(event) {
    this.setState({ 
      icon_id: event.currentTarget.dataset.id 
    }, () => console.log(icon_id));
},

You need to a little change your code. 您需要稍微更改代码。 You have to bind this method because this doesn't know about that method. 您必须绑定此方法,因为它不知道该方法。 Take a look what I changed. 看看我改变了什么。

      getInitialState: function() {
        return { icon_id: 0};
        console.log(icon_id);
    },

    onHover = (event) => {
        this.setState({ icon_id: event.currentTarget.dataset.id });
        console.log(icon_id);
    },

    render: function() {

        return (

        <ReactCSSTransitionGroup transitionName="example" transitionAppear={true} transitionLeave={true} transitionEnterTimeout={600} transitionAppearTimeout={600} transitionLeaveTimeout={300}>


                    <ul className="someclass">{ iconslist.map(function(i){
                            return <li key={i.id}><a href={i.url} target="_blank"><span className={i.class} id={i.id} data-id={i.data} onMouseOver={() => this.onHover()}></span></a></li>

                        }) }

                    </ul>
                    <p className="icon-text">{iconslist[this.state.icon_id].name}</p>


        </ReactCSSTransitionGroup>

        );

That should works, if You use arrow function You don't need to bind function. 如果使用箭头功能,则不需要绑定功能。

None of these answers were correct. 这些答案均不正确。 I figured it out. 我想到了。

Instead of using the "function" term inside the render function, I had to use: 我不得不在渲染函数中使用“函数”一词,而不必使用:

<ul className="icons">{ iconslist.map((i) => {
                            return (
                            <li key={i.id}>
                                <span className={i.class} 
                                    id={i.id} 
                                    data-id={i.data} 
                                    onMouseOver={this.onHover}>
                                </span>
                            </li>
                            )
                        }) }

                    </ul>

Using (i) => in the map function is what did it. 在map函数中使用(i)=>是什么。

No soup for anyone else in this thread. 此线程中没有其他人可以喝的汤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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