找不到变量:StyleSheet。

5
我正在学习React Native,使用的是这个网站:https://www.tutorialspoint.com/react_native/react_native_animations.htm。然而,在我尝试在iPhone上打开应用程序时出现了问题。根据错误提示,它找不到变量,尽管已经导入了该变量。
import React, { Component } from 'react';
import { View, LayoutAnimation, TouchableOpacity, Text, StyleSheet} from 'react-native';

export default class Animations extends Component {
  state = {
    myStyle: {
      height: 100,
      backgroundColor: 'red'
    }
  };
  expandElement = () => {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
    this.setState({
      myStyle: {
        height: 400,
        backgroundColor: 'red'
      }
    })
  };
  collapseElement = () => {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.linear);
    this.setState({
      myStyle: {
        height: 100,
        backgroundColor: 'red'
      }
    })
  };
  render() {
    return (
      <View>
        <View>
          <View style={this.state.myStyle}/>
        </View>

        <TouchableOpacity>
          <Text style={styles.button} onPress = {this.expandElement}>
            Expand
          </Text>
        </TouchableOpacity>

        <TouchableOpacity>
          <Text style={styles.button} onPress = {this.collapseElement}>
            Collapse
          </Text>
        </TouchableOpacity>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  button: {
    borderWidth: 1,
    borderColor: 'red',
    color: 'red',
    textAlign: 'center',
    marginTop: 50,
    padding: 10
  }
});

尝试重新编译您的项目。sudo react-native run-android - Ricky Dam
2
我喜欢在使用iOS的同时为Android编译应用程序,这是我的爱好。 - Sergey
哈哈,不好意思。sudo react-native run-ios xD 哈哈 - Ricky Dam
因为它直接在我的手机上运行,所以不会做任何事情。每次重新加载应用程序时都会重新编译。 - Sergey
1个回答

9

啊...我找到了问题所在。它在另一个组件中,该组件具有样式但没有元素,并且没有导入StylesSheet。自从我根据新条件进行了更正,但忘记了带有样式的块。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接