Created
October 7, 2025 09:55
-
-
Save tom-sherman/1239db77763af9d52d6f387975b362e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useTransition } from "react"; | |
| function App() { | |
| const [cities, setCities] = useState<string[]>([]); | |
| const [isPending, startTransition] = useTransition(); | |
| return ( | |
| <> | |
| <select | |
| onChange={(e) => { | |
| const state = e.currentTarget.value; | |
| startTransition(async () => { | |
| const cities = await getCities(state); | |
| startTransition(() => { | |
| setCities(cities); | |
| }); | |
| }); | |
| }} | |
| > | |
| <option /> | |
| {Object.keys(stateCitites).map((state) => ( | |
| <option key={state}>{state}</option> | |
| ))} | |
| </select> | |
| <select disabled={isPending}> | |
| {isPending ? ( | |
| <option>Loading... </option> | |
| ) : ( | |
| cities.map((city) => <option key={city}>{city}</option>) | |
| )} | |
| </select> | |
| </> | |
| ); | |
| } | |
| const stateCitites: Record<string, string[]> = { | |
| Utah: ["Salt Lake City", "Provo", "West Valley City"], | |
| California: ["Los Angeles", "San Francisco", "San Diego"], | |
| Texas: ["Houston", "Dallas", "Austin"], | |
| Florida: ["Miami", "Orlando", "Tampa"], | |
| "New York": ["New York City", "Buffalo", "Rochester"], | |
| }; | |
| async function getCities(state: string) { | |
| await new Promise((res) => setTimeout(res, 150)); | |
| return stateCitites[state] ?? []; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment