Vinay
Vinay

Reputation: 343

React Native Error - Invariant Violation: Element type is invalid

I am following a Udemy course to build a demo react native application. So far, the application consists of 3 components - App, AlbumList & AlbumDetail. The code for each is listed below.


App.js

import React, { Component } from 'react';
import View from 'react-native';
import AlbumDetail from './AlbumDetail'
import axios from 'axios';

class AlbumList extends Component {

  constructor(props){
    super(props)
    this.state = { albums: [{"title": "one"}, {"title": "two"}] }
  }

  renderAlbums() {
    return this.state.albums.map(album =>
      <AlbumDetail key={album.title} album={album} />
    );
  }

  render() {
    return (
      <View>
        { this.renderAlbums() }
      </View>
    );
  }
}

export default AlbumList;

AlbumList.js

import React, { Component } from 'react';
import View from 'react-native';
import AlbumDetail from './AlbumDetail'
import axios from 'axios';

class AlbumList extends Component {

  constructor(props){
    super(props)
    this.state = { albums: [{"title": "one"}, {"title": "two"}] }
  }

  renderAlbums() {
    return this.state.albums.map(album =>
      <AlbumDetail key={album.title} album={album} />
    );
  }

  render() {
    return (
      <View>
        { this.renderAlbums() }
      </View>
    );
  }
}

export default AlbumList;

AlbumDetail.js

import React from 'react';
import { View, Text } from 'react-native';

const AlbumDetail = (props) => {
  return (
    <View>
      <Text>{props.album.title}</Text>
    </View>
    )
};

export default AlbumDetail;

package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "eject": "expo eject"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "expo": "^32.0.0",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0"
  },
  "private": true
}

When I run npm start and launch the app in a mobile device(One Plus 3T running Android 8.0.0), I get the following error

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

This error is located at:
    in AlbumList (created by App)
    in RCTView (at View.js:44)
    in App (at withExpoRoot.js:22)
    ...
    ...

I went through a few questions posted in StackOverflow and most of them refer to incorrect way of exporting and importing components. As far as I can see, I am exporting and importing components correctly here but still get the error.

Feel free to add a comment if you need more information. Any help debugging the issue is appreciated.

Upvotes: 2

Views: 748

Answers (1)

Jaydeep Galani
Jaydeep Galani

Reputation: 4961

import statement is wrong here at App.js and AlbumDetail.js

change it with

import {View} from "react-native"

Upvotes: 4

Related Questions