简体   繁体   English

React Native - 如何比较 shouldComponentUpdate 中的两个 Animated.Values?

[英]React Native - How to compare two Animated.Values in shouldComponentUpdate?

I am optimizing my flatlist items (progressive images) and to avoid extending React.PureComponent I have decided to implement my own shouldComponentUpdate life-cycle method.我正在优化我的平面列表项(渐进式图像)并避免扩展 React.PureComponent 我决定实现我自己的 shouldComponentUpdate 生命周期方法。

My progressive images have two animated values:我的渐进式图像有两个动画值:

 1. thumbnailOpacity 2. imageOpacity

And my should component update must look something like this:我的应该组件更新必须如下所示:

state = {
   isLoading: true,
   thumbnailOpacity: new Animated.Value(0),
   imageOpacity: new Animated.Value(0)
}

shouldComponentUpdate(nextProps, nextState) {
  /*
    React's PureComponent implement a shouldComponentUpdate with shallow comparison.
    This is expensive here, because it need to check all our props. For a good bit-level performance,
    use the strictest rules for our list item.

    This component must re-render only when the state change.
  */

  return (
     nextState.isLoading !== this.state.isLoading ||
     nextState.thumbnailOpacity !== this.state.thumbnailOpacity ||
     nextState.imageOpacity !== this.state.imageOpacity
  );        
}

But as Animated values are not primitive data types, I don't know how to do that without a shallow comparison.但是由于动画值不是原始数据类型,如果没有浅层比较,我不知道如何做到这一点。

Any ideas?有任何想法吗? Thank you.谢谢你。

From your state initialisation:从您的状态初始化:

state = {
   ...
   thumbnailOpacity: new Animated.Value(0),
   imageOpacity: new Animated.Value(0)
}

You should be able to access it like this:您应该能够像这样访问它:

this.state.thumbnailOpacity._value

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

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