【问题标题】:Invariant Violation: Element type is invalid: React NativeInvariant Violation:元素类型无效:React Native
【发布时间】:2018-08-28 13:09:42
【问题描述】:

我在 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>
      );
    };

【问题讨论】:

  • 您的代码在这里工作正常,snack.expo.io/ryZBZCMPQ 检查您的 Login.js 中的按钮导入语句,路径是否正确?
  • 是的,我明白了!但它在系统中不起作用。你可以在你的 IDE 中尝试一下吗?
  • 文件在你的系统上,我如何在我的系统上试一下,你确定路径正确吗?
  • 发现问题。实际上是因为我没有在 button.js 中正确导出。所以我从 const 中删除了导出并在底部的导出默认按钮处插入;它工作了????

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


【解决方案1】:

在您的 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

希望这会有所帮助!

【讨论】:

  • 按照您的建议是否仍然面临同样的错误:-(
  • 这是我的问题。谢谢!
【解决方案2】:

我认为问题在于您导出 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

【讨论】:

  • 谢谢!它也有效! :-)
  • 如果这个答案解决了问题。如果你接受答案会更好。
  • 不,当我尝试它时没有工作它会给出相同的错误,所以导出默认按钮 const Button = ({ text, onPress }) = 它工作了?
猜你喜欢
  • 2017-09-16
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2020-05-10
  • 1970-01-01
  • 2019-12-05
相关资源
最近更新 更多