首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-native中传递transform属性样式的变量?

如何在react-native中传递transform属性样式的变量?
EN

Stack Overflow用户
提问于 2017-07-30 22:12:50
回答 1查看 2.3K关注 0票数 0

我尝试在react-native中传递transform属性样式的变量,但它不起作用。我的代码如下:

代码语言:javascript
复制
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native';
import { Accelerometer, Gyroscope } from 'react-native-sensors';
import { sensors } from 'react-native-sensors';

export default class Sensors extends Component {

constructor(props) {
  super(props);
  this.state = {
    rotate: 0
  }
}

render() {
  var degree = this.state.rotate;
  return (
    <View style={styles.container}>
      <View style={styles.fleche}></View>
    </View>
  );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  fleche: {
    width: 5,
    height: 150,
    backgroundColor: '#000000',
    borderRadius: 5,
    transform: [{ rotate: '' + this.state.rotate + 'deg' }],
 }
});

AppRegistry.registerComponent('Compass', () => Sensors);

我不知道在转换样式中传递变量的方法。目前,我有这个错误: undefine不是一个对象(计算'this.state.rotate')。

EN

回答 1

Stack Overflow用户

发布于 2017-07-30 22:46:02

试试这个:

代码语言:javascript
复制
render(){
    const transform = [{ rotate: this.state.rotate + 'deg' }]
    var degree = this.state.rotate
    return (
        <View style={styles.container}>
            <View style={[styles.fleche, transform]} />
        </View>
    )
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45400726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档