简体   繁体   English

在悬停时更改img React重新渲染所有子项

[英]Change img on hover React re-renders all children

I need to change the url of the img on hover. 我需要在悬停时更改img的url。 But the function trigger all of the children thats render. 但该函数会触发所有渲染的子节点。 I couldn't find a way to make the function trigger each children separate. 我找不到让这个函数触发每个孩子分开的方法。 I try to make some new state to handle the indexes, but it didn't work... 我尝试创建一些新的状态来处理索引,但它不起作用......

const Team = React.createClass ({

    getInitialState : function() {
      return { hovered: false }
    },

    componentDidMount(){
        this.props.fetchTeam();
        document.title = "Equipe | [ Adesign";
    },

    onMouseOver : function () {
      this.setState({ hovered:true });
    },

    onMouseOut : function () {
      this.setState({ hovered:false });
    },

    render(){
        return (
            <div className="wrapper">
                <div className="Team">
                    <HeaderAbout />
                    <h1>EQUIPE</h1>
                    <div className="content-team">
                    {this.props.posts.team.map((singleTeam, i) => {
                        var imgUrl = singleTeam.acf.photo.url;
                        if (this.state.hovered) {
                            imgUrl = singleTeam.acf.gif.url;
                        } else {
                            imgUrl = singleTeam.acf.photo.url;
                        }
                        return(
                            <div key={i} className="single-team col-xs-12 col-sm-6 col-md-4" onMouseOver={this.onMouseOver} onMouseOut={this.onMouseOut}>
                                <img className="img-responsive" src={imgUrl}/>
                                <p>{singleTeam.title.rendered}</p>
                            </div>
                        )
                    })}
                    </div>
                </div>
            </div>
        )
    }
});

You want to checkout the shouldComponentUpdate method : 您想要签出shouldComponentUpdate方法

Invoked before rendering when new props or state are being received. 在收到新的道具或州时,在渲染之前调用。 This method is not called for the initial render or when forceUpdate is used. 初始渲染或使用forceUpdate时不调用此方法。

Use this as an opportunity to return false when you're certain that the transition to the new props and state will not require a component update. 当您确定转换到新的道具和状态不需要更新组件时,请将此作为返回false的机会。

In order to avoid rerendering all the images you can create a component to render an image that contains the state and the event handlers. 为了避免重新渲染所有图像,您可以创建一个组件来渲染包含状态和事件处理程序的图像。 By doing so, you prevent to rerender the parent component and its siblings whenever an image is hovered. 通过这样做,您可以防止在图像悬停时重新呈现父组件及其兄弟组件。

Edit: I just realized that your code changes all the images when any of them is hovered. 编辑:我刚刚意识到,当任何图像悬停时,您的代码会更改所有图像。 Are you sure that it is what you want? 你确定它是你想要的吗? In that case, you need to rerender everything. 在这种情况下,你需要重新渲染一切。 My solution is only valid if you only want to change the hovered image, leaving the others intact. 我的解决方案仅在您只想更改悬停图像时保持有效,而其他图像保持不变。

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

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