首页 > 解决方案 > 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"

你有什么解决办法吗?

标签: javascriptreactjsleafletreact-leaflet

解决方案


解决了这个问题。该问题与react-leafletor无关props。看来我正在使用react-domversion 16.0.0。更新了我的react-dom版本16.4.1,我的问题现在解决了。


推荐阅读