Easily fetch countries and their states with optional React dropdowns
npm install country-state-dropdown

A lightweight, easy-to-use package for retrieving country and state/region data with optional React dropdown components.
- ð Comprehensive list of countries and their states/regions
- ð Simple API to fetch countries and corresponding states
- âïļ Ready-to-use React dropdown components
- ðŠķ Lightweight with zero dependencies
- ð ïļ Fully customizable styling options
- ðĶ ESM support
``bash`
npm install country-state-dropdown
or
`bash`
yarn add country-state-dropdown
`javascript
import {
getAllCountries,
getStatesByCountryCode,
} from "country-state-dropdown";
// Get all countries (returns array of objects with name and code)
const countries = getAllCountries();
// Example output: [{ name: "United States", code: "US" }, { name: "Canada", code: "CA" }, ...]
// Get states for a specific country by country code
const usStates = getStatesByCountryCode("US");
// Example output: ["California", "Texas", "New York", ...]
`
`jsx
import { CountryDropdown, StateDropdown } from "country-state-dropdown";
function LocationForm() {
const [country, setCountry] = useState("");
const [state, setState] = useState("");
return (
$3
Both dropdown components accept standard HTML attributes, including className for custom styling:
`jsx
className="my-custom-dropdown"
style={{ width: "100%", padding: "8px" }}
/>
`API Reference
$3
| Function | Parameters | Return Value | Description |
| ------------------------------ | -------------- | ------------------------------------- | --------------------------------------------------------------------- |
|
getAllCountries() | None | Array<{name: string, code: string}> | Returns all countries with their names and 2-letter ISO country codes |
| getStatesByCountryCode(code) | code: string | Array | Returns array of state/region names for the specified country code |$3
#### CountryDropdown
| Prop | Type | Required | Description |
| -------- | -------- | -------- | ----------------------------------------------------------------------- |
| value | string | No | The selected country code |
| onChange | function | Yes | Handler function that receives the selected country code as an argument |
#### StateDropdown
| Prop | Type | Required | Description |
| ----------- | -------- | -------- | ---------------------------------------------------------------- |
| countryCode | string | Yes | The country code to load states for |
| value | string | No | The selected state |
| onChange | function | Yes | Handler function that receives the selected state as an argument |
| disabled | boolean | No | Automatically disabled when no countryCode is provided |
Supported Countries
Currently includes 20+ countries with their states/regions, including:
- ðšðļ United States
- ðĻðĶ Canada
- ðŽð§ United Kingdom
- ðĶðš Australia
- ðĐðŠ Germany
- ðŦð· France
- ðŪðģ India
- ðŊðĩ Japan
- ðģðŽ Nigeria
- ð§ð· Brazil
- ðĻðģ China
- ð·ðš Russia
- ðŋðĶ South Africa
- _and many more..._
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)This project is licensed under the MIT License - see the LICENSE file for details.
Dennis Temoye