This is not an issue
This post is to document a react-leaflet wrapper for Leaflet.Geodesic implemented with strict typescript rules. It was troublesome enough that I would have preferred that someone had already done it. There is an existing project at react-leaflet-geodesic, but it was not typescript friendly; and it uses a method that is far enough behind the current version of react-leaflet that it was unusable (for me).
Context
Using Leaflet.Geodesic with react-leaflet in a Next.JS project with typescript.
The following component is what I used to form the wrapper.
Important notes:
Don't use useRef, use useState; you need this component to react to changes in the Geodesic Line.
You can use a normal function or a FunctionalComponent, they both work; but FC is a little more verbose to typescript.
The interfaces are for the typing of the props. Unfortunately,
i. type GeodesicLineProps = Parameters<typeof geodesic>;, and
ii. type GeodesicLineProps = ConstructorParameters<typeof GeodesicLineClass>
leave the props encased in an array: which I could not find a way to use as props for the wrapper that was good for use as react
props. This might be a candidate to export from the project in future.
Leaflet.Geodesic exports both GeodesicLine (a class to be instantiated with new) and the function geodesic, but geodesic was not recognized as a function by typescript at runtime.
Purpose
This is not an issue This post is to document a react-leaflet wrapper for
Leaflet.Geodesic
implemented with strict typescript rules. It was troublesome enough that I would have preferred that someone had already done it. There is an existing project at react-leaflet-geodesic, but it was not typescript friendly; and it uses a method that is far enough behind the current version ofreact-leaflet
that it was unusable (for me).Context
Using
Leaflet.Geodesic
withreact-leaflet
in a Next.JS project with typescript.Versions
React Component
The following component is what I used to form the wrapper. Important notes:
useRef
, useuseState
; you need this component to react to changes in the Geodesic Line.type GeodesicLineProps = Parameters<typeof geodesic>;
, and ii.type GeodesicLineProps = ConstructorParameters<typeof GeodesicLineClass>
leave the props encased in an array: which I could not find a way to use as props for the wrapper that was good for use as react props. This might be a candidate to export from the project in future.Leaflet.Geodesic
exports bothGeodesicLine
(a class to be instantiated withnew
) and the functiongeodesic
, butgeodesic
was not recognized as a function by typescript at runtime.Usage
OR
Where: The above is nested below a
MapContainer
and the following are imported and defined: