reactjs - 如何从 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)
解决方案
推荐阅读
- c# - 在 EditorWindow 脚本上出现两个奇怪的异常。任何想法如何解决它以及是什么造成的?
- r - 在 R 中:如何创建一个具有初始值的列,并且每个下一个值都是前一个值加上一个集合整数
- python - 右键单击不适用于在 IDLE 中设置断点。他们改变了这个功能吗?
- javascript - .then(function) 不运行函数
- .net - 实体框架 - 通过映射表加载带有标签的帖子
- sql - 如何修复 ORA-06575: 包或函数处于无效状态错误
- apache-spark - Spark 覆盖删除 db2 中已存在表的权限
- sql-server - 在删除所有记录并在 SSIS 中重新加载之前备份表
- flutter - StreamBuilder snapshot.hasError 在键盘显示/隐藏颤动时显示多次
- python - 从 MySQL 表中解密 Fernet 密文