react-leaflet库中的地图组件给出“不变违规:元素类型无效”错误

时间:2019-10-11 15:39:37

标签: javascript reactjs leaflet react-leaflet

react-leaflet的版本从1.9.1更新到了2.0.0,并出现了一个奇怪的错误:

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

Check the render method of `Map`.

我需要至少将react-leaflet升级到版本2,以便可以使用withLeaflet函数在react-leaflet内渲染矢量切片。相关文章:https://stackoverflow.com/a/53317460/6399631

在我的项目中包含Map组件的文件;

import React, {Component} from "react";
import classNames from "classnames";
import {Map} from "react-leaflet";

class FdMap extends Component {
    mapRef = (map) => {
        if (map) {
            this.mapLeaflet = map.leafletElement;
            map.leafletElement.scrollWheelZoom.disable();

            map.leafletElement.on("focus", function () {
                map.leafletElement.scrollWheelZoom.enable();
            });

            map.leafletElement.on("blur", function () {
                map.leafletElement.scrollWheelZoom.disable();
            });
        }
    };

    render() {
        const {
          className,
          children
        } = this.props;
        const containerClassName = classNames(
          "fd-map",
          className
        );

        return (
            <Map ref={this.mapRef}
                 {...this.props}
                 className={containerClassName}>
                {children}
            </Map>
        );
    };
}

export default FdMap;

我正在使用;

"react": "16.4.1",
"leaflet": "1.3.1"

你有什么解决办法吗?

1 个答案:

答案 0 :(得分:0)

解决了此问题。该问题与react-leafletprops无关。看来我正在使用react-dom版本16.0.0。将我的react-dom版本更新为16.4.1,现在我的问题已解决。