简体   繁体   English

在Android上反应Native setState()和动画

[英]React Native setState() & Animated on Android

I'm working on 'toast' like component for my react-native app. 我正在为我的本机应用程序开发“ toast”之类的组件。 And it works fine with the iOS emulator, but it doesn't update the state before the animation on my android device (Galaxy Note 2). 而且它可以在iOS模拟器上正常工作,但不会在我的android设备(Galaxy Note 2)上的动画之前更新状态。 Here is the renderer of my component: 这是我的组件的渲染器:

    render() {
        return (<Animated.View
                      style={[{transform:[{translateY: this.animatedValue}]},
                      styles.toast,
                      {backgroundColor: this.state.color}]}>
                   <Text style={styles.toastText}>{this.state.message}</Text>
                </Animated.View>);
    }

this.animatedValue is created like this on the constructor: this.animatedValue是在构造函数上这样创建的:

this.animatedValue = new Animated.Value(-70);

And I have a show method that updates the state with this.setState to set the message and color, before starting the animation with Animated.timing I have a callback on animation end which launches a timeout to hide the toast, as I said it all works fine on iOS, but on android the first time the toast is shown it is with no text in it (I initialized message as an empty string) and the second time it should show it, shows the message and color of the last toast it should have shown. 我有一个show方法,用this.setState更新状态以设置消息和颜色,然后使用Animated.timing开始动画,我在动画结束时有一个回调,它会启动超时以隐藏吐司,正如我所说的那样在iOS上可以正常工作,但是在android上,第一次显示吐司没有任何文本(我将message初始化为空字符串),第二次应该显示它,显示消息和最后一次吐司的颜色应该已经显示了。 Ex. 防爆。 You do a login fail and I see on the adb console that a failed login message is being launched, still I see an empty one with green color, then I do a correct login, and I see in the adb console that a success login message is being launched, still I see the failed message with red color instead :\\. 您登录失败,并且在adb控制台上看到正在启动失败的登录消息,但仍然看到一个空的绿色,然后我进行了正确的登录,并且在adb控制台中看到了成功的登录消息正在启动,但我仍然看到失败的消息,而是显示红色:\\。

I'm figuring it can be a state change not being fully updated before the animation starts (Sometimes I've seen the toast launch with one message and in the middle/end of the animation it suddenly changed color and text), so I've first tried starting the animation on the setState callback, that supposedly is called after the state is fully changed and rendered the component, but I still I see the empty message toast, ironically just before the animation launch I see on my adb console the logged new state and is correctly updated, but not rendered :\\ , I also tried forcing the update just after the setState with this.forceUpdate all before launching the show animation, with no success, I still get the empty message and still see the correct state in my console log :\\, I even tried setting a timeout after the setState and forceUpdate to give time to the react environment to render the component or something, before the animation, but I still get the empty message and the correct state bei 我想这可能是状态变化在动画开始之前未完全更新(有时我看到吐司启动时只显示一条消息,并且在动画的中间/结尾突然改变了颜色和文字),所以我ve首先尝试在setState回调上启动动画,该动画应该在状态完全更改并渲染组件后调用,但是我仍然看到空消息吐司,具有讽刺意味的是,在动画启动之前,我在adb控制台上看到了日志新状态,并且已正确更新,但未呈现:\\,我还尝试在setState使用this.forceUpdate强制执行更新, this.forceUpdate再启动显示动画,但没有成功,我仍然收到空消息,仍然看到正确的状态在我的控制台日志中:\\,我什至尝试在setStateforceUpdate之后设置一个超时,以便在动画之前给反应环境一些时间来渲染组件或其他东西,但是我仍然得到空消息和正确的状态bei ng shown on my console log D: ng显示在控制台日志D上:

I know it must be something obvious or silly, but I just can't figure it out :(, I've been working with react native for like 2~3 weeks, I know javascript for a long time, and I think I get little grasp of the react native components life cycle, but nothing more, I'm still new to react native, so any help would be greatly appreciated, thanks xD. 我知道这一定是显而易见的或愚蠢的,但是我只是想不起来:(,我已经使用React native进行了大约2到3周的工作,我已经很长时间了解javascript了,我想我明白了很少了解react native组件的生命周期,但仅此而已,我仍然是React Native的新手,因此非常感谢xD,感谢您的帮助。

所以我弄清楚了发生了什么,很明显,如果组件完全不在视野范围内,它不会重新渲染它(有意义,没有要渲染的东西),然后在最后一个渲染的组件上开始动画(这就是我的意思)认为是错误的,应在再次可见后立即重新渲染它),因此我通过使组件具有透明背景的1像素可见(1像素行)来对其进行了修补,因此在状态更新时将其重新渲染将要显示并制作动画,然后将其隐藏,然后将其背景再次设置为透明,以隐藏1像素行,希望这对那里的人有所帮助,我不知道在哪里发布问题,因此他们将其固定为XD ,祝你好运。

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

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