不变违规:元素类型无效:React Native

时间:2018-08-28 13:09:42

标签: javascript android react-native react-native-android react-native-ios

4-5天前,我开始学习react native,现在我在React Native中建立登录屏幕。但是面对这个问题,我试图解决这个问题,但不幸的是,我不能这样做。大家能帮我吗?我将在此处附加我的代码和屏幕截图。Error Screenshot

App.js
import React, { Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Login from './src/screens/Login';


export default class App extends Component {
  render() {
    return (
       <Login />
    );
  }
}

这是登录屏幕代码

Login.js


  import React, { Component} from 'react';
import { StyleSheet, TextInput, View, Image, StatusBar, Dimensions } from 'react-native';
import { LinearGradient } from 'expo';
import EStyleSheet from 'react-native-extended-stylesheet';
import { Button }  from '../components/Button';
import { Ionicons } from '@expo/vector-icons';

const {width: WIDTH} = Dimensions.get('window')

EStyleSheet.build();

export default class Login extends Component {
  render() {
    return (
      <LinearGradient
      colors={['#38E870', '#2BB256']} 
      style={styles.container}>
      <StatusBar barStyle="light-content" />
      <View style={styles.logocontainer}>
        <Image source={require('../images/Devastic.png/')} />
      </View>
      <View style={styles.formContainer}>
      <View style={styles.inputcontainer}>
      <Ionicons name="ios-person-outline" size={36} color="#747475" 
      style={styles.inputemail}/>
           <TextInput 
           placeholder={'Enter Your Email'}
           underlineColorAndroid={'transparent'}
           placeholderTextColor="#dfe6e9"
           style={styles.input}
           />
           <Ionicons name="ios-unlock-outline" size={34} color="#747475" 
      style={styles.inputpass}/>
           <TextInput 
           placeholder={'Enter Your Password'}
           secureTextEntry={true}
           underlineColorAndroid={'transparent'}
           placeholderTextColor="#dfe6e9"
           style={styles.input}
           />     
           <Button
           text="Log In"
           onPress={() => console.log ("Pressed")}
            />
            </View>
      </View>
      </LinearGradient>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    },
    logocontainer: {
        paddingTop: 50,
    },
    formContainer: {
      backgroundColor: '#FFF',
      marginTop: 180,
      width: 360,
      height: 340,
      borderRadius: 10,
      shadowColor: '#000',
      shadowOffset: { width: 0, height: 2 },
      shadowOpacity: 0.6,
      shadowRadius: 6,
      elevation: 8,
      },
      inputcontainer: {
        marginTop: 30,
      },
      inputemail: {
               position: 'absolute',
               left: 18,
               top: 13,
      },
      inputpass: {
        position: 'absolute',
               left: 18,
               top: 77,
      },

      input: {
      width: WIDTH -50 ,
      height: 44,
      padding: 10,
      paddingLeft: 40,
      marginVertical: 10,
      marginHorizontal: 10,
      borderWidth: 1,
      borderRadius: 20,
      borderColor: 'black',
      marginBottom: 10,
      }
});

这是Button.js文件代码

import React from 'react';
    import { View, TouchableHighlight, TouchableNativeFeedback, Text, Platform } from 'react-native';
    import EStyleSheet from 'react-native-extended-stylesheet';

    const styles = EStyleSheet.create({
      $buttonColor: '#38E870',
      $buttonTextColor: '#ffffff',
      $buttonUnderlayColor: '#2BB256',
      button: {
        backgroundColor: '$buttonColor',
        paddingVertical: 10,
        paddingHorizontal: 30,
        marginHorizontal: 12,
        borderRadius: 40,
        marginTop: 20,
        },
      text: {
        color: '$buttonTextColor',
        fontSize: 18,
        textAlign: 'center',
        fontWeight: 'bold',
      },
    });

    export const Button = ({ text, onPress }) => {
      if (Platform.OS === 'ios') {
        return (
          <TouchableHighlight
            onPress={onPress}
            style={styles.button}
            underlayColor={styles.$buttonUnderlayColor}
          >
            <Text style={styles.text}>{text}</Text>
          </TouchableHighlight>
        );
      }

      return (

        <TouchableNativeFeedback
          onPress={onPress}
          background={TouchableNativeFeedback.Ripple(styles.$buttonUnderlayColor)}
        >
          <View style={styles.button}>
            <Text style={styles.text}>{text}</Text>
          </View>
        </TouchableNativeFeedback>
      );
    };

2 个答案:

答案 0 :(得分:0)

我认为是有问题的,因为您导出Button组件的方式。

export foo;
import {foo} from 'blah';


export default foo;
import foo from 'blah';

解决问题的方法

替换此

export const Button = ({ text, onPress }) => 

与此

export default const Button = ({ text, onPress }) => 

有关更多详细信息,请检查此more detail

答案 1 :(得分:0)

在Login.js文件中,您正在将Button导入为对象。

更改如下:

来自

import { Button }  from '../components/Button';

收件人

import Button from '../components/Button';

有关更多信息,请查看:Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

希望这会有所帮助!