Library Components built with @react-leaflet/core cannot useLeafletContext
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.
- [x] All peer dependencies are installed: React, ReactDOM and Leaflet.
- [x] Using a supported version of React and ReactDOM (v17.0.0 minimum).
- [x] Using the supported version of Leaflet (v1.7.1 minimum) and its corresponding CSS file is loaded.
- [x] Using the latest v3 version of React-Leaflet.
- [x] The issue has not already been reported.
- [x] Make sure you have followed the quick start guide for Leaflet.
- [x] Make sure you have fully read the documentation and that you understand the limitations.
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
- create custom component:
import { createPathComponent } from '@react-leaflet/core';
import L from 'leaflet';
function getBounds(props) {
return L.latLng(props.center).toBounds(props.size)
}
function createSquare(props, context) {
const instance = new L.Rectangle(getBounds(props))
return { instance, context: { ...context, overlayContainer: instance } }
}
function updateSquare(instance, props, prevProps) {
if (props.center !== prevProps.center || props.size !== prevProps.size) {
instance.setBounds(getBounds(props))
}
}
const Square = createPathComponent(createSquare, updateSquare)
export default Square;
- Publish component or link to component from another respository
- Use component in
- Get Error:
No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>
https://codepen.io/MichaelACohen/pen/yLbewVX - example using someones MarkerClusterGroup
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 3.2.1 to 3.2.2.
I rolled back the update to 3.2.1. Now the map renders again.
Recognized that the issue is somehow with the react-leaflet-fullscreen-plugin. When I remove the plugin, react-leaflet 3.2.2 works well.
Got the error after updating react-leaflet from 3.2.1 to 3.2.2. I'm also using react-leaflet-markercluster
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??
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.
Same here. I downgraded to 3.21,
according to this thread: https://github.com/yuzhva/react-leaflet-markercluster/issues/156 I should try to downgrade it further to 3.0.2
Still does not help
I´m also facing this issue when i use the useMap() hook
Working when i downgrade to :
- react < 18 (because react 18 works only with react-leaflet < v4 released a few days ago
- "react-leaflet": "3.1.0",
- "react-leaflet-control": "^2.1.2",
- "react-leaflet-draw": "^0.19.8",
- "react-leaflet-markercluster": "^3.0.0-rc1",
@amauryfischer Did you downgrade React to a version lower than 18 as well or just the leaflet libs?
Getting the same issue with
- NextJS 12,
- ReactJS 18,
- react-leaflet 4.0.0
- react-leaflet-markercluster 3.0.0-rc1
- react-universal 2.2.1
@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 ^^ Well I guess I will wait until react-marker-cluster upgrades to react-leaflet v4... Have you seen any info whether someone is working on that?
@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.
- @geoman-io/leaflet-geoman-free": "^2.11.4"
- "@react-leaflet/core": "^1.1.1"
- "@types/leaflet": "^1.7.9"
- "leaflet": "^1.8.0" -"react": "^18.1.0"
- "react-leaflet": "^4.0.0"
Same issue on upgrading react-leaflet to v4.
- @geoman-io/leaflet-geoman-free": "^2.11.4"
- "@react-leaflet/core": "^1.1.1"
- "@types/leaflet": "^1.7.9"
- "leaflet": "^1.8.0" -"react": "^18.1.0"
- "react-leaflet": "^4.0.0"
Solved by upgrading @react-leaflet/core to v2.0.0
ui there is hope :) Will check and report back!
Nope so far no luck with that...
ui there is hope :) Will check and report back!
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:
{
"name": "react-leaflet-markercluster",
"version": "3.0.0-rc1",
"description": "React wrapper of Leaflet.markercluster for react-laeflet",
"main": "dist/index.js",
"repository": "[email protected]:YUzhva/react-leaflet-markercluster.git",
"keywords": [
"react",
"leaflet",
"react-leaflet",
"markercluster",
"marker",
"cluster"
],
"author": "Yevhen Uzhva <[email protected]>",
"license": "MIT",
"private": false,
"scripts": {
"dev": "start-storybook -p 8080",
"lint": "eslint --ext .js ./ --ignore-pattern '/dist/'",
"build:code": "babel src -d dist && terser ./dist/react-leaflet-markercluster.js -o dist/index.js && rm ./dist/react-leaflet-markercluster.js",
"build:styles": "node-sass src/styles.scss dist/styles.min.css --output-style compressed",
"build:source": "yarn build:code && yarn build:styles",
"build:gh-pages": "build-storybook",
"deploy:gh-pages": "gh-pages -d storybook-static"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@storybook/addon-actions": "^5.3.18",
"@storybook/addon-docs": "^5.3.18",
"@storybook/preset-scss": "^1.0.2",
"@storybook/react": "^5.3.18",
"babel-loader": "^8.1.0",
"css-loader": "^3.4.2",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^2.5.1",
"gh-pages": "^2.2.0",
"husky": "^4.2.3",
"node-sass": "^4.13.1",
"sass-loader": "^8.0.2",
"style-loader": "^1.1.3",
"terser": "^4.6.10"
},
"dependencies": {
**"@react-leaflet/core": "^1.0.2",**
**"leaflet": "^1.6.0",**
**"leaflet.markercluster": "^1.4.1",**
**"react-leaflet": "^3.0.0"**
},
"peerDependencies": {
**"leaflet": "^1.6.0",**
**"leaflet.markercluster": "^1.4.1",**
**"react-leaflet": "^3.0.0"**
},
"husky": {
"hooks": {
"pre-commit": "yarn lint"
}
}
}
My Project setup has the following though:
leaflet: "^1.7.1",
leaflet.markercluster: "^1.5.3",
@react-leaflet/core: 2.0
react-leaflet: 4.0.0
react-leaflet-markercluster: 3.0.0-rc1
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: react-leaflet-markercluster. I also downgrade react-leafet to v3.0.5 and still get this error due to unknown reason! Please notice the current package.json module version below:
"react": "^17.0.2",
"react-dom": "^17.0.2",
"leaflet": "^1.8.0",
"react-leaflet": "^4.0.0",
"react-leaflet-markercluster": "^3.0.0-rc1",
@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 but i have the same error :/