【发布时间】:2020-07-03 01:03:20
【问题描述】:
我是 React 的新手。我制作了一个带有按钮和图像网址列表的小应用程序。单击按钮时,图像 url 将添加到列表中。我使用标准.map 函数呈现图像网址列表。
我想在图像显示时制作一个快速的ui动画效果:淡入和从左滑入的组合。我尝试了 Velocity.js 并找到了 velocity-react 包装器。但我无法理解如何使用它。 “标准”velocity-animate 库也是如此。
什么是最好的? velocity-react、velocity-animate 还是别的什么?
我该怎么做?
JSX
<div className="row">
{
this.state.images.map( (image, index) => { return this.renderThumb(image, index); } )
}
</div>
renderThumb 函数
renderThumb(image, index) {
return (
<div ref="tweetImage" key={`image-${index}`} className="col-xs-3 tweetImage">
<img className="img-thumbnail" src={image} alt="my pic"/>
</div>
);
}
速度反应
我尝试像这样将 <img> 动画不透明度从 0 包装到 1(从 docs 复制):
<VelocityComponent animation={{ opacity: 1 }} duration={ 500 }>
<img className="img-thumbnail" src={image} alt="my pic"/>
</VelocityComponent
我不断收到此错误:
警告:React.createElement: type is invalid -- 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:对象
ReactCSSTransitionGroup 也没有运气(如下面的建议)。显示图像但没有动画:
renderThumb(image, index) {
return (
<div ref="tweetImage" key={`image-${index}`} className="col-xs-3">
<ReactCSSTransitionGroup
transitionName="example">
<img className="img-thumbnail" src={image} alt="Ole Frank Jensen"/>
</ReactCSSTransitionGroup>
</div>
);
}
已解决:
我将 <ReactCSSTransitionGroup transitionName="example"> 移到了衰落组件之外,瞧 :-)
渲染()
<div className="row">
<ReactCSSTransitionGroup transitionName="example">
{
this.state.images.map( (image, index) => { return this.renderThumb(image, index); } )
}
</ReactCSSTransitionGroup>
</div>
renderThumb()
renderThumb(image, index) {
return (
<div key={`image-${index}`} className="col-xs-3">
<img className="img-thumbnail" src={image} alt="Ole Frank Jensen"/>
</div>
);
}
【问题讨论】:
-
注意
this.state.images.map( (image, index) => { return this.renderThumb(image, index); } )可以写成this.state.images.map( this.renderThumb )
标签: reactjs animation css-animations velocity.js reactcsstransitiongroup