【问题标题】:Getting React Runtime error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)获取 React 运行时错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件)
【发布时间】:2021-12-08 15:42:14
【问题描述】:

这是我的代码的样子:

import { React, useState, useEffect } from 'react';
import { GoogleMapReact } from 'google-map-react';
import styles from './Location.module.scss';
import pinstyles from './TheMap.module.scss';
import { useIntl } from 'react-intl';

/* This file has the actual with the coordinates passed to it */
const TheMap = (props) => {
  // for holding the API key and language
  const bootstrapURLKeys = {
    //should this be french?
    key: process.env.NEXT_PUBLIC_API_KEY,
    language: 'en',
  };

  //const [test, setTest] = useState(false);

  //   const marker = new google.maps.Marker({
  //     position: props.center,
  //     map,
  //   });

  // To do with the language of the map
  const { formatMessage: f } = useIntl();

  return (
    <div className={styles.main}>
      <div className={styles.heading}> {f({ id: 'location' })} </div>
      <div className={styles.mapContainer}>
        {/* TODO: The defaultZoom should be working but it isn't, not 
        a big deal but fix when there's time (it'll look better) */}
        <GoogleMapReact
          bootstrapURLKeys={bootstrapURLKeys}
          defaultZoom={props.zoom}
          center={props.center}
          yesIWantToUseGoogleMapApiInternals
          options={{ fullscreenControl: false }}
        >
          <Marker lat={props.lat} lng={props.lng} />
        </GoogleMapReact>
      </div>
      {/* Adding the address to the bottom of the map */}
      {props.location[1].address}
      {props.location[1].postalCode}
    </div>
  );
};

const Marker = (props) => {
  return (
    <>
      <div className={pinstyles.pin}></div>
      <div className={pinstyles.pulse}></div>
    </>
  );
};

export default TheMap;

这是完整的错误:

未处理的运行时错误

错误:元素类型无效:应为字符串(对于内置 组件)或类/函数(用于复合组件),但得到: 不明确的。您可能忘记从文件中导出组件 它是在其中定义的,或者您可能混淆了默认导入和命名导入。

我想知道如何摆脱这个错误。

编辑:这是我的调用堆栈的样子:

【问题讨论】:

    标签: javascript reactjs google-maps-api-3 runtime


    【解决方案1】:

    您的GoogleMapReact 导入错误。应该是:

    import GoogleMapReact from 'google-map-react';
    

    https://github.com/google-map-react/google-map-react#getting-started

    【讨论】:

    • 是的,这是我原来的,我改变了它,看看它是否能修复错误。但是,将其改回并不能消除错误,所以我认为不是这样。
    • 我注意到您缺少 Marker 导入。那时大概就是这样。我已经相应地更新了答案。
    • 哦,从头开始。我滚动得不够远。 :) 抱歉,不知道发生了什么。
    • 您可以在return 之前执行console.log(GoogleMapReact) 并查看它记录的内容。除非您为了简洁而删除了一些代码,否则 GoogleMapReactMarker 是您使用的仅有的两个不是纯 HTML 标记的东西,所以在这种情况下,这些是唯一可能的故障点,imo。
    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2020-11-06
    • 2020-04-14
    • 1970-01-01
    • 2018-10-28
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多