0

I started to learning react native a 4-5 days ago and now I am building a login screen in React Native. But facing this issue, I am trying to figure it out but unfortunately, I couldn't do so. Can guys please help me? I am attaching my code and screenshot Here.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 />
    );
  }
}

Here is the Login screen code

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,
      }
});

here is the Button.js File code

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>
      );
    };
user9824674
  • 493
  • 1
  • 14
  • 28
  • your code work fine here, https://snack.expo.io/ryZBZCMPQ check your button import statement in your Login.js, is the path correct? – slashsharp Aug 28 '18 at 13:43
  • Yes I see! But it's not working in the system. Can you try it in your IDE? – user9824674 Aug 28 '18 at 14:51
  • the file is on your system, how can i try it on mine, have you make sure the path is correct? – slashsharp Aug 28 '18 at 14:52
  • Found the issue. Actually it was in I haven't export properly in button.js. So I removed the export from const and insert at the bottom export default button; And it worked – user9824674 Aug 28 '18 at 15:36

2 Answers2

1

In your Login.js file, you are importing Button as object.

Change as following:

From

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

To

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

For more information, please have a look : Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

Hope this will help !

sdn404
  • 614
  • 5
  • 7
0

I think the issue because the way you have exported the Button component.

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


export default foo;
import foo from 'blah';

solution to your problem

Replace this

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

with this

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

for more detail check this more detail

Mohammed Ashfaq
  • 3,353
  • 2
  • 15
  • 22