react-leaflet-ant-path icon indicating copy to clipboard operation
react-leaflet-ant-path copied to clipboard

TypeError: Super expression must either be null or a function

Open Thanasist2 opened this issue 4 years ago • 4 comments

"leaflet": "^1.7.1",
"leaflet-ant-path": "^1.3.0",
"react-leaflet": "^3.0.0",
"react-leaflet-ant-path": "^1.1.0",

It stoped working after react-leaflet 3 install

Νέα εικόνα bitmap

Thanasist2 avatar Nov 05 '20 00:11 Thanasist2

I think you don't need this library. You can just use @react-leaflet/[email protected] and the native [email protected]. In my example it works like this:

import { useLeafletContext } from '@react-leaflet/core'
import { antPath } from 'leaflet-ant-path';

export default () => {

    const AntPath = (p) => {
        const context = useLeafletContext()
        useEffect(() => {
            let antPolyline = antPath(p.positions, p.options);
            context.map.addLayer(antPolyline)
            return () => {
                context.map.removeLayer(antPolyline)
            }
        })
        return null
    }
    
    ...

    return (
        <Map>
            <AntPath positions={...} options={}> </AntPath>
        </Map>
    )
}


Of yourse, this is just a quick and dirty example, but you can find your way from here.

caritasverein avatar Nov 09 '20 16:11 caritasverein

Thank you very match, your code is extremely useful!

Thanasist2 avatar Nov 09 '20 19:11 Thanasist2

Thank you :-). After some time i came to this. Works a lot better, does not rerender so often and supports children for tooltips and popups.

import { createElementHook, createPathHook, createContainerComponent } from '@react-leaflet/core'
import { antPath } from 'leaflet-ant-path';

function createAntPath(props, context) {
    const instance = antPath(props.positions, props.options)
    return { instance, context: { ...context, overlayContainer: instance } }
}


const useAntPathElement = createElementHook(createAntPath, () => {})
const useAntPath = createPathHook(useAntPathElement)
const AntPath = createContainerComponent(useAntPath)

export default AntPath

caritasverein avatar Nov 09 '20 19:11 caritasverein

a little improvement so be able to update the component

import { createElementHook, createPathHook, createContainerComponent } from '@react-leaflet/core'
import { antPath } from 'leaflet-ant-path';

function createAntPath(props, context) {
    const instance = antPath(props.positions, props.options)
    return { instance, context: { ...context, overlayContainer: instance } }
}

function updateAntPath(instance, props, prevProps) {
    if (props.positions !== prevProps.positions || props.options !== prevProps.options) {
        instance.setLatLngs(props.positions)
    }
}

const useAntPathElement = createElementHook(createAntPath, updateAntPath)
const useAntPath = createPathHook(useAntPathElement)
const AntPath = createContainerComponent(useAntPath)

export default AntPath

Thanasist2 avatar Nov 09 '20 22:11 Thanasist2