This npm package provides a collection of utility functions designed to simplify the retrieval of geographical data, specifically information about states, districts, sub-districts, and villages. The package is designed to work with a JSON dataset, allowi
npm install geodata-utilsbash
npm install geodata-utils
`
Usage
`javascript
import React, { useState } from "react";
import {
useFetchStates,
useFetchDistrictsByState,
useFetchSubDistrictsByDistrict,
useFetchVillagesBySubDistrict,
} from "geodata-utils";
const CheckLocation = () => {
const [selectedState, setSelectedState] = useState("");
const [selectedDistrict, setSelectedDistrict] = useState("");
const [selectedSubDistrict, setSelectedSubDistrict] = useState("");
const [selectedVillage, setSelectedVillage] = useState("");
const states = useFetchStates();
const districts = useFetchDistrictsByState(selectedState);
const subDistricts = useFetchSubDistrictsByDistrict(
selectedState,
selectedDistrict
);
const villages = useFetchVillagesBySubDistrict(
selectedState,
selectedDistrict,
selectedSubDistrict
);
const handleStateChange = (event) => {
setSelectedState(event.target.value);
setSelectedDistrict("");
setSelectedSubDistrict("");
setSelectedVillage("");
};
const handleDistrictChange = (event) => {
setSelectedDistrict(event.target.value);
setSelectedSubDistrict("");
setSelectedVillage("");
};
const handleSubDistrictChange = (event) => {
setSelectedSubDistrict(event.target.value);
setSelectedVillage("");
};
const handleVillageChange = (event) => {
setSelectedVillage(event.target.value);
};
return (
<>
id="districts"
onChange={handleDistrictChange}
value={selectedDistrict}
>
{districts.map((district, index) => (
))}
id="subDistricts"
onChange={handleSubDistrictChange}
value={selectedSubDistrict}
>
{subDistricts.map((subDistrict, index) => (
))}
id="villages"
onChange={handleVillageChange}
value={selectedVillage}
>
{villages.map((village, index) => (
))}
>
);
};
export default CheckLocation;
`
API
useFetchStates()
Returns an array of all states.
useFetchDistrictsByState(stateName)
Returns an array of all districts in the specified state.
stateName: Name of the state as a string.
useFetchSubDistrictsByDistrict(stateName, districtName)
Returns an array of all sub-districts in the specified district and state.
stateName: Name of the state as a string.
districtName: Name of the district as a string.
useFetchVillagesBySubDistrict(stateName, districtName, subDistrictName)
Returns an array of all villages in the specified sub-district, district, and state.
stateName: Name of the state as a string.
districtName: Name of the district as a string.
subDistrictName`: Name of the sub-district as a string.