javascript - 不变违规 : Element type is invalid:

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

您好,我在创建按钮组件时遇到此问题,这是错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

这是登录,js代码文件

 import React, { Component} from 'react';
import { StyleSheet, Text, View, Image, StatusBar, TextInput, Dimensions } from 'react-native';
import { LinearGradient } from 'expo';
import { Button }  from './components/button';
import { Ionicons } from '@expo/vector-icons';
import PropTypes from 'prop-types';

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




export default class Login extends Component {
  render() {
    return (
      <LinearGradient
      colors={['#38E870', '#2BB256']} 
      style={styles.container}>
      <StatusBar barStyle="light-content" />
      <View style={styles.logocontainer}>

      </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="Click Me"
        onPress={() => {
            alert("Hi there!!!");
        }}
        />
            </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, { Component } from 'react';
import PropTypes from 'prop-types';
import { TouchableOpacity, Text, StyleSheet } from 'react-native';

class Button extends Component {
  render() {
    const { text, onPress } = this.props;
    return (
      <TouchableOpacity style={styles.buttonStyle} onPress={() => onPress()}>
        <Text style={styles.textStyle}>{text}</Text>
      </TouchableOpacity>
    );
  }
}

Button.propTypes = {
  text: PropTypes.string.isRequired,
  onPress: PropTypes.func.isRequired,
};

const styles = StyleSheet.create({
  textStyle: {
    fontSize: 20,
    color: '#ffffff',
    textAlign: 'center',
  },

  buttonStyle: {
    padding: 10,
    backgroundColor: '#202646',
    borderRadius: 5,
  },
});

export default Button;

我错过了什么?一个函数?

最佳答案

您需要将按钮导入为

从“./components/button”导入 Button,因为它已作为默认导出导出

或将按钮导出为命名导出

关于javascript - 不变违规 : Element type is invalid:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52085398/

相关文章:

React-Native iOS - 无法读取未定义的属性 'logInWithReadPermissions'

React-native Fetch GET,同时将请求项传递到 url

javascript - 共享点 : Submit form data from Edit form and then redirect to display form

javascript - 我必须通过 jQuery 删除父 div

javascript - 如何绕过 promise 链中的 'then' 的其余部分,就像传统的 'return' 语句一样?

node.js - npm build 如何通过变量或脚本更改 package.json - 主页值

javascript - JQuery .data() 在更改值后不获取值

django - 在 native react 中获取 csrf token

reactjs - 如何将 React bootstrap NavBar 向左对齐

react-native - 平面列表中的文本离开屏幕 react native