Build a Dependent Prefecture and City Select in React
When you build a dependent prefecture and city dropdown in React, don't keep the city options in state. Calculate them from the selected prefecture on each render. Then reset the city selection to empty when the prefecture changes, so the screen and the state never disagree. This approach works for any form where the first dropdown narrows the options of the second, such as a sign-up form or a shipping address form.
In my test environment, I built three versions: one that doesn't reset the city selection, one that resets it in an event handler, and one that resets it in useEffect. For each, I recorded what was shown on screen, the state, and the values sent with the form. I also tried a version that fetches the city list from a server.
The test environment is Ubuntu 24.04 with Node.js 22.23.4. I used react and react-dom 19.3.1, @types/react and @types/react-dom 19.3.0, typescript 7.0.2, esbuild 0.28.2, and jsdom 31.0.0. I type-checked the .tsx files with tsc, transpiled them with esbuild, and ran them on jsdom. The GIF was recorded by running the same components in Chromium 153, which came with Playwright 1.63.0. I rendered the components without wrapping them in <StrictMode>, and I counted commits with <Profiler>. The data has only three prefectures, each with three cities. The server is a fake I wrote for testing, and it doesn't reply until I signal it to. I did not test national data, an address search API, or combinations with form libraries.
Calculate the city options from the prefecture on each render
I made the data an object keyed by prefecture, where each value is an array of cities. As the TypeScript 4.9 release notes on the satisfies operator explain, satisfies checks that an expression matches a type without changing the expression's type. So Prefecture becomes the union of the keys: '東京都' | '大阪府' | '福岡県'.
// Small sample data. Only some cities are listed for each prefecture.
export const CITIES = {
東京都: ['千代田区', '新宿区', '八王子市'],
大阪府: ['大阪市', '堺市', '豊中市'],
福岡県: ['福岡市', '北九州市', '久留米市'],
} satisfies Record<string, string[]>;
export type Prefecture = keyof typeof CITIES;
export const PREFECTURES = Object.keys(CITIES) as Prefecture[];
export function isPrefecture(value: string): value is Prefecture {
return value in CITIES;
}
The component keeps only the selected prefecture and city in state. The city options cities can be computed from the prefecture, so they are not kept in state. The React docs on values you can calculate from props or state also recommend computing such values during rendering rather than storing them in state.
import { useState, type ChangeEvent } from 'react';
import { CITIES, PREFECTURES, isPrefecture, type Prefecture } from './data';
export function DependentSelect() {
const [prefecture, setPrefecture] = useState<Prefecture | ''>('');
const [city, setCity] = useState('');
// Don't keep the city options in state; compute them from the prefecture on each render
const cities = prefecture ? CITIES[prefecture] : [];
function handlePrefectureChange(e: ChangeEvent<HTMLSelectElement>) {
const value = e.target.value;
setPrefecture(isPrefecture(value) ? value : '');
setCity(''); // When the prefecture changes, reset the city selection to empty
}
return (
<form>
<select name="prefecture" value={prefecture} onChange={handlePrefectureChange}>
<option value="">都道府県を選択</option>
{PREFECTURES.map((p) => <option key={p} value={p}>{p}</option>)}
</select>
<select name="city" value={city} onChange={(e) => setCity(e.target.value)}
disabled={!prefecture}>
<option value="">市区町村を選択</option>
{cities.map((c) => <option key={c} value={c}>{c}</option>)}
</select>
<output>state: {prefecture || '(なし)'} / {city || '(なし)'}</output>
</form>
);
}
Until a prefecture is chosen, the city dropdown is disabled so it can't be selected. For testing, I show the state inside an <output> element.
Here is the record of selecting Tokyo, then selecting Shinjuku-ku, then changing the prefecture to Osaka. "Screen" is what the dropdowns show. "Sent values" is the contents of the FormData built from the <form>. "Commits" is how many times React committed the change to the screen for that action.
$ node run.mjs DependentSelect
最初:
画面: 都道府県=都道府県を選択 市区町村=市区町村を選択(選べない) 選択肢=[市区町村を選択]
state: (なし) / (なし) 送信される値: prefecture=
コミット: 1回
東京都を選んだあと:
画面: 都道府県=東京都 市区町村=市区町村を選択 選択肢=[市区町村を選択,千代田区,新宿区,八王子市]
state: 東京都 / (なし) 送信される値: prefecture=東京都&city=
コミット: 1回
新宿区を選んだあと:
画面: 都道府県=東京都 市区町村=新宿区 選択肢=[市区町村を選択,千代田区,新宿区,八王子市]
state: 東京都 / 新宿区 送信される値: prefecture=東京都&city=新宿区
コミット: 1回
都道府県を大阪府に変えたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
コミット: 1回
When the prefecture changes to Osaka, the options switch to Osaka's cities, and the city goes back to the "Select a city" placeholder. In the initial state, the disabled city dropdown is not included in FormData (only prefecture= appears).
Without resetting the city, the screen and the state disagree
I made StaleCity by removing only setCity('') from the code above. The prefecture onChange calls only setPrefecture. I ran the same steps.
$ node run.mjs StaleCity
(東京都と新宿区を選ぶところまでは DependentSelect と同じなので省略)
都道府県を大阪府に変えたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / 新宿区 送信される値: prefecture=大阪府&city=
コミット: 1回
The city on screen shows the "Select a city" placeholder again, but the state still holds Shinjuku-ku. The state is "Osaka / Shinjuku-ku", even though Shinjuku-ku is not an option for Osaka. The values sent from the <form> are city= (empty), which matches the screen. A version that sends the values from state instead of FormData would send "Osaka / Shinjuku-ku".
The <select> reference says that the string passed to value must match the value of one of the <option> elements inside. After the prefecture changes, StaleCity no longer meets this condition.
The following GIF shows StaleCity (①) and DependentSelect (②) stacked vertically in the browser, with the same selections made in the same order. The top of the screen shows the step number as a counter in the form "step 1 of 3" (the label is in Japanese), followed by what is being selected. The moment a dropdown is clicked, a pink circle appears around the cursor. Right after a selection, the field whose value changed turns pink. On the third step, when the prefecture changes to Osaka, ① keeps Shinjuku-ku in its state, and ② goes back to (none). At the end, the result text and both states are highlighted in pink.

Resetting the city in useEffect renders the stale combination once
You can also reset the city when the prefecture changes by using useEffect:
useEffect(() => {
setCity('');
}, [prefecture]);
I added this useEffect to StaleCity to create EffectReset, and I recorded the state on each render.
$ node run.mjs EffectReset
(東京都と新宿区を選ぶところまでは省略)
都道府県を大阪府に変えたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
描画: 大阪府 / 新宿区 → 描画: 大阪府 / (なし)
コミット: 2回
The final state is "Osaka / (none)", the same as DependentSelect. However, the component first renders with the combination "Osaka / Shinjuku-ku", and then renders again after setCity('') runs inside useEffect. That's two commits to the screen. The React docs example of adjusting some state when a prop changes also explain that resetting state in an Effect first renders the old value and then renders again.
The prefecture changes only when the user operates the dropdown. If the city should be reset at the same moment, call setCity('') in the onChange event handler, as DependentSelect does. In the DependentSelect record above, switching to Osaka made one commit, and the state was "Osaka / (none)" at that point.
When fetching the city list from a server, discard stale replies
Instead of keeping every city in the country in your code, you can fetch the cities from a server each time a prefecture is selected. The React docs data-fetching example explain that fetching belongs in an Effect because it keeps the data in sync with the server while the component is shown, according to the current selection. Here too, the city list is fetched in useEffect based on the selected prefecture.
I tested a case where Tokyo is selected, the prefecture is changed to Osaka before the Tokyo reply arrives, and the server replies to Osaka first and then to Tokyo. The fake server's fetchCities doesn't reply until I signal it, so I can control the order of the replies.
First is AsyncNoIgnore, which passes the fetched list directly to setCities:
useEffect(() => {
if (!prefecture) return;
fetchCities(prefecture).then((list) => {
setCities(list);
});
}, [prefecture]);
$ node run.mjs AsyncNoIgnore
(最初から、大阪府を選んだ直後までは省略)
大阪府の返事が先に届いたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
サーバー: 要求:東京都 → 要求:大阪府 → 返事:大阪府
コミット: 1回
東京都の返事が遅れて届いたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,千代田区,新宿区,八王子市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
サーバー: 要求:東京都 → 要求:大阪府 → 返事:大阪府 → 返事:東京都
コミット: 1回
The late Tokyo reply overwrote the list. The prefecture is Osaka, but the city options show Tokyo's wards and cities.
The same data-fetching example calls this a race condition. It shows a cleanup function in the Effect that ignores stale replies. AsyncCities follows that approach:
import { useEffect, useState, type ChangeEvent } from 'react';
import { PREFECTURES, isPrefecture, type Prefecture } from './data';
import { fetchCities } from './server';
export function AsyncCities() {
const [prefecture, setPrefecture] = useState<Prefecture | ''>('');
const [city, setCity] = useState('');
const [cities, setCities] = useState<string[]>([]);
useEffect(() => {
if (!prefecture) return;
let ignore = false;
fetchCities(prefecture).then((list) => {
if (!ignore) setCities(list); // Ignore the reply to an old request
});
return () => {
ignore = true;
};
}, [prefecture]);
function handlePrefectureChange(e: ChangeEvent<HTMLSelectElement>) {
const value = e.target.value;
setPrefecture(isPrefecture(value) ? value : '');
setCity('');
setCities([]);
}
const loading = prefecture !== '' && cities.length === 0;
return (
<form>
<select name="prefecture" value={prefecture} onChange={handlePrefectureChange}>
<option value="">都道府県を選択</option>
{PREFECTURES.map((p) => <option key={p} value={p}>{p}</option>)}
</select>
<select name="city" value={city} onChange={(e) => setCity(e.target.value)}
disabled={cities.length === 0}>
<option value="">{loading ? '読み込み中…' : '市区町村を選択'}</option>
{cities.map((c) => <option key={c} value={c}>{c}</option>)}
</select>
<output>state: {prefecture || '(なし)'} / {city || '(なし)'}</output>
</form>
);
}
As described in the useEffect reference on parameters, when a dependency value changes, React calls the cleanup function with the previous value before running the Effect with the new value. So the ignore flag for the Tokyo request becomes true once the render for Osaka is committed. In handlePrefectureChange, I also reset the city list along with the city selection. While the list is loading, the dropdown shows "Loading…" and can't be selected.
$ node run.mjs AsyncCities
(最初の状態は省略)
東京都を選んだ直後:
画面: 都道府県=東京都 市区町村=読み込み中…(選べない) 選択肢=[読み込み中…]
state: 東京都 / (なし) 送信される値: prefecture=東京都
サーバー: 要求:東京都
コミット: 1回
東京都の返事を待たずに大阪府を選んだ直後:
画面: 都道府県=大阪府 市区町村=読み込み中…(選べない) 選択肢=[読み込み中…]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府
サーバー: 要求:東京都 → 要求:大阪府
コミット: 1回
大阪府の返事が先に届いたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
サーバー: 要求:東京都 → 要求:大阪府 → 返事:大阪府
コミット: 1回
東京都の返事が遅れて届いたあと:
画面: 都道府県=大阪府 市区町村=市区町村を選択 選択肢=[市区町村を選択,大阪市,堺市,豊中市]
state: 大阪府 / (なし) 送信される値: prefecture=大阪府&city=
サーバー: 要求:東京都 → 要求:大阪府 → 返事:大阪府 → 返事:東京都
コミット: 0回
The Tokyo reply did arrive from the server, but the options stayed as Osaka's cities, and React made no commit for it.
This code does not include response caching or merging of duplicate requests. The useEffect reference section on alternatives to fetching data in Effects recommends using your framework's data-fetching mechanism if you have one. Otherwise, consider a client-side cache such as TanStack Query or useSWR.





