-
Notifications
You must be signed in to change notification settings - Fork 891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Library Components built with @react-leaflet/core cannot useLeafletContext #897
Comments
Please provide an example codepen that doesn't use a third-party library to reproduce the issue. |
Got the same error after updating react-leaflet from Recognized that the issue is somehow with the |
Got the error after updating react-leaflet from 3.2.1 to 3.2.2. |
I'm having the same issue when trying to use react-leaflet-draw in a working react-leaflet map. Downgrading to 3.2.1 didn't fix the issue. |
@smcalilly tengo el mismo problema, utilizo react leaflet 2.8 y react leaflet draw 0.19.0 |
@smcalilly pudiste solucionar el error?? |
Same here. I downgraded to 3.21, according to this thread: yuzhva/react-leaflet-markercluster#156 |
Still does not help |
1. Completed actionables 2. Tailwind configured 3. Leaflet is also configured with react-leaflet Issues: 1. React-leaflet-draw is having runtime issues, link to a similar issue: PaulLeCam/react-leaflet#897
I´m also facing this issue when i use the useMap() hook |
Working when i downgrade to :
|
@amauryfischer Getting the same issue with
|
@l4b4r4b4b4 Yes i downgraded to React 17 from 18 because only react leaflet v4 is compatible with react 18 |
yeah, I was afraid that was the case and figured it out by trying too ^^ |
@l4b4r4b4b4 not yet unfortunately :/ guess we will have to do it ourself if nothing is done in coming month |
Same issue on upgrading react-leaflet to v4.
|
Solved by upgrading @react-leaflet/core to v2.0.0 |
ui there is hope :) Nope so far no luck with that... |
Digging down on the installed react-marker-cluster in node_modules I found the following in package.json of the module:
My Project setup has the following though:
Maybe package dependencies of the module simply need to be updated and it will work? |
@PaulLeCam It seems it has conflict with some third-party modules such as:
|
@l4b4r4b4b4 i tried to fork react leaflet markercluster and update its dependecies to react leaflet v4 and react-leaflet core v2 with a fork : https://github.com/amauryfischer/react-leaflet-markercluster |
Thats unlucky. I currently have my head stuck in authentication and authorization with keycloak, openLDAP and next-auth, which needs most of my attention, but will quickly have a look at it. Storybook Github Page Deployment Update 1I noticed quite a few other packages needed an upgrade as well.
I noticed react and react-dom missing as dependencies, which I found odd, so I added them as dev dependencies as stated in this github issue. Now I get different compilation errors especially for not having an appropriate file loader:
Update 2
Still same error 😢 Update 3Got the storybook to run. Here are the steps as far as I remember them:
Update 4
When simply importing the code into It complains about invalid hook calls as well as the map context. I guess the issues is around how Update 5I think I'm close. I refactored the whole code interfacing to leaflet.markercluster in the style of the article referred to above. Now I'm stuck at Leaflet not loading leaflets DistantGrid contructor.
No matter whether trying the @react/leaflet/core hooks or plain implementation in useEffect hook.
Update 6Ok. I got it to compile without errors with above code. Passing an empty update function to `` seems to have been causing the constructor issue... But the cluster feature still shines with absence 😜 My guess I would have to take a closer look at the needed props and potentially leaflet.markcluster's update function. |
As documented here (PaulLeCam/react-leaflet#897 (comment)) several people me included had difficulties making it work with newer versions of the ecosystem. I finally got it to work with above code and following system settings. docker-compose on Debian "next": "^12.1.5", "react-dom": "^18.1.0", "react": "^18.1.0", "leaflet": "^1.8.0", "leaflet-defaulticon-compatibility": "^0.1.1", "leaflet.markercluster": "^1.5.3", "@react-leaflet/core": "^2.0.0", "@types/leaflet": "^1.7.9", "react-leaflet": "^4.0.0",
I tried @changey/react-leaflet-markercluster (forked from https://github.com/l4b4r4b4b4/react-leaflet-markercluster) and it seems to work fine as a drop-in replacement. |
When markercluster is used, I now sometimes get this error: @l4b4r4b4b4 Maybe I deleted yarn.lock, deleted node_modules, then ran As they are preceded by ^ in the package.json, this should not happen with new installations. But did with installations previous to the last update of react-leaflet/core (2.0.1) and react-leaflet (1.0.2). |
I can confirm this is working great with all last version of react-leaflet and leaflet |
I am glad original maintainer confirms :D |
If some people still get this error after upgrading react-leaflet and/or leaflet, i manage to fix it by removing the plugin (in my case the marker cluster plugin) and re-add it. |
Example above were not working for me with preact, so this is what I came up with: // MarkerClusterGroup.tsx
import { type MarkerClusterGroupOptions, MarkerClusterGroup as LeafletMarkerClusterGroup } from 'leaflet'
import { type EventedProps, createLayerComponent, createElementObject, extendContext } from '@react-leaflet/core'
export interface MarkerClusterGroupProps extends MarkerClusterGroupOptions, EventedProps {
children?: React.ReactNode
}
export const MarkerClusterGroup = createLayerComponent<LeafletMarkerClusterGroup, MarkerClusterGroupProps>(
function createMarkerCluster({ children: _c, eventHandlers, ...options }, context) {
const markerClusterGroup = new LeafletMarkerClusterGroup(options)
return createElementObject(markerClusterGroup, extendContext(context, { layerContainer: markerClusterGroup }))
},
function updateMarkerCluster(markerClusterGroup, props, prevProps) {
if (props.children !== prevProps.children) {
markerClusterGroup.refreshClusters()
}
}
) Usage: // MapPage.tsx
export const MapPage: FunctionComponent<{
devices: Array<{ id: string, position: { lat: number, lng: number } }>
}> = ({
devices
}) =>
<MapContainer>
<MarkerClusterGroup
eventHandlers={{
// @ts-ignore - Cluster events are not typed, but that's a different story
clusterclick: () => console.log('click')
}}
>
{devices.map(device =>
<Marker key={device.id} position={device.position}>
)}
</MarkerClusterGroup>
</MapContainer> |
Bug report
Before opening an issue, make sure to read the contributing guide and understand this is a bug tracker, not a support platform.
Please make sure to check the following boxes before submitting an issue.
Issues opened without using this template will be closed unless they have a good reason not to follow this template.
Expected behavior
Expect component built with @react-leaflet/core to render.
Actual behavior
React app will not compile with message:
No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>
Steps to reproduce
No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>
https://codepen.io/MichaelACohen/pen/yLbewVX - example using someones MarkerClusterGroup
The text was updated successfully, but these errors were encountered: