按照高级组件工厂的官方参考来更新控制组件的道具
核心 API 导出其他可以以类似方式使用的高级组件工厂。
我模仿了这个例子 - 但我收到以下语法错误:
import L from "leaflet";
import "leaflet-routing-machine";
import { createControlComponent } from "@react-leaflet/core";
import 'leaflet-routing-machine/dist/leaflet-routing-machine.css'
function setWaypoints(props)
{
return {
waypoints: [
L.latLng(props.startLat, props.startLng),
L.latLng(props.endLat, props.endLng)
],
lineOptions: {
styles: [{ color: "#0500EE", weight: 4 }]
},
show: false,
addWaypoints: false,
routeWhileDragging: true,
draggableWaypoints: true,
fitSelectedRoutes: true,
showAlternatives: false,
createMarker: function() { return null; },
}
}
function createRoutingMachine(props, context)
{
const instance = new L.Routing.control(setWaypoints(props))
return
{
instance, context: { ...context, overlayContainer: instance }
}
}
function updateRoutingMachine(instance, props, prevProps)
{
if (props.endLat !== prevProps.endLat || props.endLng !== prevProps.endLng)
{
instance.setWaypoints(props)
}
}
const RoutingMachine = createControlComponent(createRoutingMachine, updateRoutingMachine)
export default RoutingMachine;
缺少分号。(35:25)
33 | 返回 34 | {
35 | 实例,上下文:{ ...上下文,overlayContainer:实例}
| ^ 36 | }
如果我将其更改为:
function createRoutingMachine(props)
{
const instance = new L.Routing.control(setWaypoints(props))
return instance
}
编译器很高兴,但组件永远不会更新。
我知道我错误地创建了控制组件,但我找不到正确实现的信息。
相关:
如何在 React-Leaflet 3 中使用 Leaflet Routing Machine?
如何在 react-leaflet v3 中扩展 TileLayer 组件?