ios - 在React Native中出现“未定义不是对象(评估StyleSheet.create)”错误

标签 ios reactjs react-native react-native-ios

我是React Native的新手,我正在尝试在正在创建的应用程序中使用滑动导航。我已经安装了randomcolor以及native-react-swipe,但是由于某些原因,我仍然遇到错误。

import React, {Component} from 'react';
import Swiper from 'react-native-swiper'
import randomcolor from 'randomcolor'

const {
  View,
  Text,
  StyleSheet
} = React

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  view: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  }
});

class TitleText extends React.Component {
  render() {
    return (
      <Text style={{ fontSize: 48, color: 'white' }}>
        {this.props.label}
      </Text>
    )
  }
}

class Home extends React.Component {

  viewStyle() {
    return {
      flex: 1,
      backgroundColor: randomcolor(),
      justifyContent: 'center',
      alignItems: 'center',
    }
  }

  render() {
    return (
      <Swiper
        loop={false}
        showsPagination={false}
        index={1}>
        <View style={this.viewStyle()}>
          <TitleText label="Left" />
        </View>
        <Swiper
          horizontal={false}
          loop={false}
          showsPagination={false}
          index={1}>
          <View style={this.viewStyle()}>
            <TitleText label="Top" />
          </View>
          <View style={this.viewStyle()}>
            <TitleText label="Home" />
          </View>
          <View style={this.viewStyle()}>
            <TitleText label="Bottom" />
          </View>
        </Swiper>        
        <View style={this.viewStyle()}>
          <TitleText label="Right" />
        </View>
      </Swiper>

    )
  }
}

export default Home

我得到的错误是“未定义不是对象(正在评估'StyleSheet.create')”。我已经尝试破解了一段时间,但没有任何运气。
那就是我拥有的代码,任何帮助将不胜感激!

最佳答案

为了访问 React Native components ,您需要从react-native而不是react导入它们

import {
  View,
  Text,
  StyleSheet
} from 'react-native'

关于ios - 在React Native中出现“未定义不是对象(评估StyleSheet.create)”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52064331/

相关文章:

ios - 将字符串值传递给 viewcontroller 但得到 EXC_BAD_ACCESS

javascript - React - 处理状态数组内单元格的修改

javascript - 如何使用 React Navigation 导航到渲染函数之外的新屏幕,而不使用 JSX?

ios - 获取解析 PFUser : relational data

ios - 尝试在我的 iOS 应用程序中使用 LinkedIn 身份验证进行身份验证

ios - 当另一个警报出现时,alertview 关闭

javascript - 如何防止我的功能组件使用 React memo 或 React hooks 重新渲染?

javascript - 如何在 react 中设置单选按钮的默认值?

javascript - 在 ReactNative 中使用 slider 滚动水平 ScrollView

ios - 如何监控 http 请求以便在我的 ios 应用程序中看到我的 header 请求