Obtain TopoJSON map files
mainreact-simple-maps requires you to provide your own valid TopoJSON or GeoJSON files. You can use these files to visualize countries, regions, or continents at various resolutions.
Recommended sources for map files:
repository·main·Indexed 25 days ago
https://github.com/zcreativelabs/react-simple-mapsAn SVG map chart component for React that uses a declarative API to render maps. It leverages d3-geo and topojson-client for rendering, panning, and zooming. The library provides core components such as ComposableMap, Geographies, Geography, Marker, and Line, as well as state management via MapProvider and ZoomPanProvider with associated hooks like useMapContext and useZoomPanContext.
react-simple-maps requires you to provide your own valid TopoJSON or GeoJSON files. You can use these files to visualize countries, regions, or continents at various resolutions.
Recommended sources for map files:
You can install react-simple-maps using npm or yarn.
$ npm install react-simple-maps$ yarn add react-simple-mapsTo render a map, use the ComposableMap, Geographies, and Geography components. You must provide a URL to a valid TopoJSON file to the geographies prop of the Geographies component. The Geographies component uses a render prop that provides an array of geographies, which you can then map over to render individual Geography components.
import React from "react";
import ReactDOM from "react-dom";
import { ComposableMap, Geographies, Geography } from "react-simple-maps";
// url to a valid topojson file
const geoUrl =
"https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json";
const App = () => {
return (
<div>
<ComposableMap>
<Geographies geography={geoUrl}>
{({ geographies }) =>
geographies.map((geo) => (
<Geography key={geo.rsmKey} geography={geo} />
))
}
</Geographies>
</ComposableMap>
</div>
);
};
document.addEventListener("DOMContentLoaded", () => {
ReactDOM.render(<App />, document.getElementById("app"));
});The MapProvider and MapContext allow you to manage and share map-related state across your component tree. You can consume this state using the useMapContext hook.
import {
MapProvider,
MapContext,
useMapContext
} from 'react-simple-maps';The following components are the primary building blocks for creating maps. They are exported from the main entry point:
ComposableMap: The top-level container for your map.Geographies: A wrapper for rendering multiple geographic features.Geography: Represents a single geographic feature (e.g., a country or state).Graticule: Renders a grid of latitude and longitude lines.Sphere: Renders a background sphere (useful for world maps).ZoomableGroup: A component that enables zooming and panning capabilities.Marker: Used to place points or icons at specific coordinates.Line: Used to draw lines between coordinates.Annotation: Used to add text or labels to the map.The ZoomPanProvider and ZoomPanContext provide state management for zoom and pan interactions. You can consume this state using the useZoomPanContext hook.
import {
ZoomPanProvider,
ZoomPanContext,
useZoomPanContext
} from 'react-simple-maps';The library provides custom hooks for interacting with map data and zoom/pan state:
useGeographies: A hook to fetch and manage geographic data.useZoomPan: A hook to interact with or control zoom and pan state.useMapContext: A hook to access the map context.useZoomPanContext: A hook to access the zoom and pan context.