2

I am in the process of upgrading my application to React 17 and using function based components. In the old version of React, everything was class based. The map in the old version used leaflet 1.7.1. However it is not rendering in the new version and is throwing a white screen.

Here's my code:

import React, { useEffect } from "react";
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
import "./NewMap.css";

export default function NewMap() {
  const dispatch = useDispatch();
  const position = [35.831, -78.767];
  return (
    <div>
      <MapContainer
        center={position}
        zoom={13}
        scrollWheelZoom={false}
      >
        <TileLayer
          attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
        <Marker position={position}>
          <Popup>
            Test.
            <br /> Lorem ipsum.
          </Popup>
        </Marker>
      </MapContainer>
    </div>
  );
}

I'm also getting the following console errors:

react.development.js?72d0:220 Warning: React.createElement: 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.

react-dom.development.js?61bb:25058 Uncaught Error: 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.

I ran npm i react-leaflet to get the latest version of React Leaflet (3.2.5) and it's still throwing these errors and showing me a white screen. Any ideas on how to fix this?

4
  • Where are you using your component? Did you accidentally do an import { NewMap } from './NewMap', when you meant to do an import NewMap from './NewMap'...the error message implies that's the issue Commented Jan 25, 2022 at 18:16
  • I'm using NewMap in another component. I imported it there properly. That's not the issue.
    – maestro777
    Commented Jan 25, 2022 at 19:47
  • Can you post your code for where its being used? Commented Jan 25, 2022 at 20:16
  • I wound up getting it to work. I had to change MapContainer to Map and use and older version of Leaflet.
    – maestro777
    Commented Jan 26, 2022 at 18:29

1 Answer 1

0

Try this

npm i react-router-dom@next

or

import {ReactDOM} from 'react-dom';

or this

import { BrowserRouter } from 'react-router-dom';
2
  • None of these solutions worked for me. However, I am seeing this new warning: ./node_modules/react-router-dom/node_modules/react-router/index.js 174:13-19"export 'Action' was not found in 'history'
    – maestro777
    Commented Jan 25, 2022 at 16:50
  • Do an````` npm i history````` Maybe the issue is with the latest version of React-Router.Change it into old version name
    – arun s r
    Commented Jan 26, 2022 at 1:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.