class AnimateItemComponent extends React.Component
{
static propTypes =
{
animate: React.PropTypes.bool
};
render()
{
let aniamte = this.props.aniamte;
let style = {};
let className = ["item-class"];
let time = parseInt(Math.random() * 2000); // Maximum of 2 seconds
if (aniamte)
{
// Consider support more prefixes
style.animationDelay = time + "ms";
}
if (this.props.active)
className.push("animate"); // Class the activate the animation
return React.createElement("div", {className, style}, "My text");
}
}
class AnimateComponent extends React.Component
{
constructor()
{
this.state = {};
// Initialize the value to keep the prop types boolean
this.state.aniamte = false;
// Our items to handle, can be easily replaced with the setState.
this.state.items = ["a", "b", "c", "d", "e"];
}
render()
{
let content = [];
let items = this.state.items;
let aniamte = this.state.aniamte;
content = items.map(item =>
{
return React.createElement(AnimateItemComponent, {animate});
});
return content;
}
startAnimation()
{
this.setState({aniamte: true});
}
stopAnimation()
{
this.setState({aniamte: false});
}
serverRequest()
{
// Activate animation:
this.startAnimation();
// Stop animation
this.stopAnimation();
}
}
출처
2016-09-05 21:03:25
Ido
고맙습니다 만, 아니요. 정확하게 두 가지 이유로 필요한 것은 아닙니다. 1. 각 render() 호출이 애니메이션을 적용 할 것입니다. 항목이 백엔드에서 반환 될 때만 애니메이션을 적용하려고합니다 (따라서 "항목"이 채워짐). 만약 render()가 다시 호출된다면 애니메이션을 원하지 않지만, 그냥 렌더링 2. 각 타일을 움직이기 원하지만, 애니메이션은 무작위로 시작해야합니다. 사용자 (무작위로 할당 할 2-3 개의 클래스가 충분할 것입니다). 어려운 점은 첫 번째 것입니다. – Bertuz
'render '가 여러 번 호출 될 수 있다는 것을 알면 매우 실망 할 것입니다. Redux를 사용하고'console.log'를'render'에 넣으면 DOM이 완전히 준비된 순간까지 콘솔에서 4-5 번 메시지를 볼 수 있습니다. 이것이 Ido가 'componentDidMount'에서 애니메이션을 시작한 이유입니다. 즉, 해당 DOM이 렌더링 된 후 한 번만입니다. –
필요에 따라 답을 수정했습니다 : – Ido