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

Library Components built with @react-leaflet/core cannot useLeafletContext

Open miccoh1994 opened this issue 5 years ago • 26 comments

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

  1. 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;
  1. Publish component or link to component from another respository
  2. Use component in
  3. 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

miccoh1994 avatar Jul 06 '21 11:07 miccoh1994

Please provide an example codepen that doesn't use a third-party library to reproduce the issue.

PaulLeCam avatar Oct 09 '21 16:10 PaulLeCam

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.

jhotadhari avatar Oct 19 '21 15:10 jhotadhari

Got the error after updating react-leaflet from 3.2.1 to 3.2.2. I'm also using react-leaflet-markercluster

piotr-cz avatar Dec 21 '21 14:12 piotr-cz

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 avatar Jan 18 '22 22:01 smcalilly

@smcalilly tengo el mismo problema, utilizo react leaflet 2.8 y react leaflet draw 0.19.0

MarceloDiazz avatar Mar 17 '22 14:03 MarceloDiazz

@smcalilly pudiste solucionar el error??

MarceloDiazz avatar Mar 17 '22 14:03 MarceloDiazz

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

neelduttahere avatar Apr 17 '22 14:04 neelduttahere

Still does not help

neelduttahere avatar Apr 17 '22 15:04 neelduttahere

I´m also facing this issue when i use the useMap() hook

manuelpoelzl avatar Apr 21 '22 18:04 manuelpoelzl

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 avatar Apr 22 '22 20:04 amauryfischer

@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 avatar Apr 24 '22 15:04 l4b4r4b4b4

@l4b4r4b4b4 Yes i downgraded to React 17 from 18 because only react leaflet v4 is compatible with react 18

amauryfischer avatar Apr 25 '22 08:04 amauryfischer

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 avatar Apr 25 '22 15:04 l4b4r4b4b4

@l4b4r4b4b4 not yet unfortunately :/ guess we will have to do it ourself if nothing is done in coming month

amauryfischer avatar Apr 25 '22 16:04 amauryfischer

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"

arnaldocrescente avatar Apr 28 '22 13:04 arnaldocrescente

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

arnaldocrescente avatar Apr 28 '22 13:04 arnaldocrescente

ui there is hope :) Will check and report back!

Nope so far no luck with that...

l4b4r4b4b4 avatar Apr 28 '22 14:04 l4b4r4b4b4

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?

l4b4r4b4b4 avatar Apr 28 '22 14:04 l4b4r4b4b4

@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",

seyyed-sina avatar May 03 '22 10:05 seyyed-sina

@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 :/

amauryfischer avatar May 09 '22 14:05 amauryfischer