株式会社シーポイントラボ | 浜松のシステム・RTK-GNSS開発

インポート間違いによるElement type is invalid: expected a stringエラーの解決方法について

react-native-mapsにマーカーを追加してビルドしたところ以下のエラーが発生。

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)
but got: undefined. You likely forgot to export your component from the file it's defined in,
or you might have mixed up default and named imports.

原因は

import { MapView, Marker} from 'react-native-maps';

と定義していたため

正しくはこう

import MapView, { Marker} from 'react-native-maps';

単純にインポート方法が違っていたためでした。

MarkerやImageといった小道具はカッコで囲み、メインライブラリであるMapViewのみ

外に出すという概念でしょうか。

確かreactをインポートするときも

import React, { useState, useEffect } from 'react';

だったのでこの法則は合っているかもしれない。

  • この記事いいね! (0)
モバイルバージョンを終了