0

i have more than 13300 points of interests to show on a map. i have created a app with starter react app, react-leaflet and react-leaflet-markercluster

but i have this error "can't access property "useState", dispatcher is null" when is on the code

import { useState, useEffect } from 'react';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import 'leaflet.markercluster/dist/MarkerCluster.css';
import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
import MarkerClusterGroup from 'react-leaflet-markercluster';

// Fix  marker icons
const defaultIcon = L.icon({
    iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
    iconUrl: require('leaflet/dist/images/marker-icon.png'),
    shadowUrl: require('leaflet/dist/images/marker-shadow.png'),
    iconSize: [25, 41],
    iconAnchor: [12, 41],
    popupAnchor: [1, -34],
    shadowSize: [41, 41]
});

function App() {
    const [pois, setPois] = useState([]);
    const [loading, setLoading] = useState(true);

    // Exemple datas
    useEffect(() => {
        const mockData = Array.from({ length: 100 }, (_, i) => ({
            id: i,
            name: `Point ${i + 1}`,
            lat: 48.86 + Math.random() * 0.02,
            lng: 2.33 + Math.random() * 0.02
        }));
        setPois(mockData);
        setLoading(false);
    }, []);



    return (
        <div style={{ height: '100vh', width: '100%' }}>
            <MapContainer
                center={[48.86, 2.33]}
                zoom={13}
                style={{ height: '100%', width: '100%' }}
            >
                <TileLayer
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                    attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
                />
                <MarkerClusterGroup>
                    {pois.map(poi => (
                        <Marker
                            key={poi.id}
                            position={[poi.lat, poi.lng]}
                            icon={defaultIcon}
                        >
                            <Popup>{poi.name}</Popup>
                        </Marker>
                    ))}
                </MarkerClusterGroup>
            </MapContainer>
        </div>
    );
}

export default App;

So how can i solve this issue? I still try to recreate the project, with different versions, but i still have the same problem

1 Answer 1

1

I believe the error is due to react-leaflet-markercluster not fully being compatible with newer React or React-Leaflet versions. You can fix it by uninstalling it and using @changey/react-leaflet-markercluster, which works with React 18.

If not just make sure your <MarkerClusterGroup> is placed inside the <MapContainer> to avoid hook context errors.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much :) probleme solve with the @changey/react-leaflet-markercluster dependencies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.