首页 > 解决方案 > 如何从 react-leaflet v2 重写到 v3?

问题描述

有一个在 react-leaflet v2 上运行的库。https://github.com/somarmeteorologia/react-leaflet-markers 但我的项目使用 react-leaflet v3。因此,这个库需要为新版本重写。为此,您需要重写以下代码:

import React from 'react'
import { MapLayer, withLeaflet } from 'react-leaflet'
import PropTypes from 'prop-types'

import { markers } from './core'

import { isEquals } from './utils'

class Markers extends MapLayer {
  constructor(props) {
    super(props)

    this.leafletElement = L.markers(props)
    this.leafletElement
      .attachLayer(props.leaflet.map)
      .attachMarkers(props.markers)
  }

  componentDidMount() {
    super.componentDidMount()
    this.leafletElement.reset()
  }

  createLeafletElement(props) {
    return L.markers(props)
  }

  updateLeafletElement(fromProps, toProps) {
    !isEquals(fromProps.markers, toProps.markers) &&
      this.leafletElement.attachMarkers(toProps.markers)
  }
}

Markers.propTypes = {
  markers: PropTypes.array.isRequired,
  isDebug: PropTypes.bool.isRequired,
  options: PropTypes.shape({})
}

Markers.defaultProps = {
  isDebug: false,
  options: {}
}

export default withLeaflet(Markers)

标签: reactjsleafletmarkersreact-leafletreact-leaflet-v3

解决方案


推荐阅读