从ReactNative中的StyleSheet.create访问状态值

Hos*_*eeb 6 reactjs react-native

我是ReactNative的新手,并且具有以下组件:

  <View style={{flex: 1}}>
    <View style={styles.container}>
      <Animated.Image style= {styles.photo}
              source={{uri: this.props.user.picture.large}}/>
    </View>
    <View style={{backgroundColor:'whitesmoke', flex: 1}}></View>
  </View>
Run Code Online (Sandbox Code Playgroud)

在样式中,我具有以下内容:

const styles = StyleSheet.create({
  container: {
  flex: 0.5,
  backgroundColor: '#4dbce9',
  alignItems: 'center',
 },
 photo: {
  height: 150,
  width: 150,
  borderRadius: 75,
  borderWidth: 3,
  borderColor: 'white',
  transform: [                        // `transform` is an ordered array
            {scale: this.state.profileImgBounceVal}, 
          ]
  }
});
Run Code Online (Sandbox Code Playgroud)

this.state.profileImgBounceVal在我知道的组件外部进行访问时,出现错误。除了在Animated.Image标记内包含样式外,是否有其他解决方法?

Sve*_*ven 6

您可以用来Stylesheet.flatten()在组件中创建可重用的样式对象:

var animatedImageStyle = StyleSheet.flatten([
  styles.photo,
  {
    transform: [{scale:this.state.profileImgBounceVal}]
  }
])

<View style={{flex: 1}}>
  <View style={styles.container}>
    <Animated.Image style={animatedImageStyle}
            source={{uri: this.props.user.picture.large}}/>
  </View>
  <View style={{backgroundColor:'whitesmoke', flex: 1}}></View>
</View>
Run Code Online (Sandbox Code Playgroud)