简体   繁体   English

reactjs-动画单个DOM元素而无需重新渲染组件

[英]reactjs - animate single DOM element without re-rendering component

I have following react component: 我有以下反应成分:

return(
            <div className="project-page">
                <div className="jumbotron">
                    <div className="container">
                        <h1>{currentProject.title}</h1>
                    </div>  
                </div>  
                <div className="container ">
                    <ReactCSSTransitionGroup transitionName="example" transitionAppear={true}>
                        <div className="project-image-container"> 
                            <img src={currentProject.image} alt="Project Image" />
                        </div>
                    </ReactCSSTransitionGroup>  
                    .....

I'm using ReactCSSTransitionGroup to animate image which works fine on components' initial load. 我正在使用ReactCSSTransitionGroup来对图像进行动画处理,该图像在组件的初始负载下效果很好。 However animation doesn't work when component gets updated with new dataset/image. 但是,当使用新的数据集/图像更新组件时,动画不起作用。

How can I make this work? 我该如何进行这项工作?

Animating updated content is not currently supported in react. 当前不支持对更新的内容进行动画处理。 Currently, ReactCSSTransitionGroup can only animate components that are being added or removed. 当前, ReactCSSTransitionGroup只能为要添加或删除的组件设置动画。

However it is possible to make react think that a component has been removed or added. 但是,可以使React认为已删除或添加了一个组件。 React relies on the key attribute to uniquely identify child components. React依靠key属性来唯一地标识子组件。

So you should be able to do something like this: 因此,您应该能够执行以下操作:

return(
    var key = projectId;

    <ReactCSSTransitionGroup key={key} transitionName="example" transitionAppear={true}>
          <div className="project-image-container"> 
               <img src={currentProject.image} alt="Project Image" />
          </div>
    </ReactCSSTransitionGroup>  

In this example, you must pass unique id or any other value. 在此示例中,您必须传递唯一的id或任何其他值。 It doesn't even need to be unique. 它甚至不必是唯一的。 You just need to update it with any value on component update. 您只需要使用组件更新中的任何值对其进行更新。

This blog covers this topic in more depth. 博客更深入地讨论了该主题。

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

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