2017-08-05 50 views
0

我对我的反应本机应用程序没有找到问题的答案。setState之后的新动画 - React Native

如果你有一个想法如何做到这一点,那将是巨大的:)

我想要做的事:

在一个页面,当我按下某个地方,我想在新闻媒介的位置上显示一个动画(例如一个正方形的幻影)。

我所取得的成就: 当我点击,一个正方形是显示与正确的位置上的动画。 但是,当我点击其他地方,广场的位置改变,但动画不会重新启动。

我曾尝试:

做动画,我把一个<查看/>(与位置:“绝对”)在印刷机上的位置。

这<查看/>在我称之为1次在我的应用程序组件包埋渲染:

<ClickAnimation x={item.x} y={item.y}/> 

其中item.x和item.y是有坐标。

这是我的组件的代码:

import React from 'react'; 
import {Animated, View} from 'react-native'; 

export default class ClickAnimation extends React.Component { 
    state = { 
    scaleAnim: new Animated.Value(0) 
    }; 
    componentWillMount() { 
    Animated 
     .timing(this.state.scaleAnim, { 
     toValue: 2, 
     duration: 500 
    }) 
     .start(); 
    } 
    componentWillUpdate(nextProps) { 
    if (nextProps.x != this.props.x && nextProps.y != this.props.y) { 
     this.setState({ 
     scaleAnim: new Animated.Value(0) 
     }) 
    } 
    } 
    componentDidUpdate() { 
    console.log("componentDidUpdate",this.state.scaleAnim) 
    Animated 
     .timing(this.state.scaleAnim, { 
     toValue: 2, 
     duration: 500 
    }) 
     .start(); 
    } 
    render() { 
    return (<Animated.View 
     style={{ 
     position: "absolute", 
     top: this.props.y, 
     left: this.props.x, 
     width: 50, 
     height: 50, 
     backgroundColor: "red", 
     transform: [ 
     { 
      scaleY: this.state.scaleAnim 
     }, { 
      scaleX: this.state.scaleAnim 
     }, { 
      translateX: -25 
     }, { 
      translateY: -25 
     } 
     ] 
    }}/>); 
    } 
} 

在componentDidUpdate中的console.log给我的每次点击2日志:

{_children:阵列(2),_value:2。 ..,_animation:空...}

{_children:阵列(2),_value:0,...,_animation:空...}

我真的不知道下一步该怎么做。

PS:在NativeScript中,这更简单。我只需将新组件添加到DOM。

回答

0

根据React文档,您不能在componentWillUpdate()中使用this.setState(),如果您需要更新状态以响应prop更改,请改用componentWillReceiveProps(nextProps)。

https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops

看了上面的链接,对更多的细节,并检查其注意事项。 我希望这是什么原因造成的问题

+0

我尝试过使用componentWillReceiveProps,但不会改变结果。 componentWillReceiveProps中的console.log显示状态正确获取数据。 – Wandrille

+1

只要尝试增加持续时间从500到5000毫秒,并检查发生了什么 –

+0

Endeed,这是工作。第二个点击动画正在显示。但动画效果不佳。没有过渡。我不知道这是世博会的问题还是德代码的问题。 – Wandrille

0

看来,世博会XDE使应用程序过慢,这就是为什么动画部件不能正常工作。