There are basically two aspects to the story here.
One is the wave of Chinese migration starting in the mid 19th century. China was a very volatile and chaotic place during that time. There were multiple wars with foreign powers, multiple civil wars, extensive exploitation of China by foreign colonial powers (e.g. Britain, France, Russia, the Dutch, even Japan), and several famines each of which would kill millions of people. Some of those problems were caused or exacerbated by essentially a failing government which did little to shepherd China into the modern industrial era. That era is called the "Century of Humiliation" in China because of all those problems. Even in the 19th century China was a very populous country, so as people started flowing out of the country looking for better lives elsewhere that translated to a lot of individuals.
That explains why "Chinatowns" exist at all, the large number of Chinese immigrants to cities meant that in many places Chinatowns were significant.
The rest of the equation is explained essentially entirely by racism. In the US the Chinese Exclusion Act was passed in the 1880s, severely limiting legal Chinese immigration to the US. This combined with other "jim crow like" laws meant that people of Chinese heritage were very much second class citizens in the US. From the 1870s through the early 1900s there were multiple anti-Chinese race riots throughout America, in Seattle, San Francisco, Wyoming, Los Angeles, and elsewhere. The wave of Pacific Coast anti-Chinese riots of 1907 were organized by anti-Chinese groups in major west coast cities in the US and Canada, and they forced Chinese residents out of certain jobs and even out of some cities entirely. Other riots were little more than anti-Chinese pogroms (and included murders of Chinese-Americans).
The overarching message transmitted by white North America and received by Chinese immigrants was that they were living in lands which were hostile to them and would use violence up to and including deadly force to keep the Chinese "in their place". Which meant preventing them from living anywhere they wanted or pursuing any job they wanted, even of sharing the same schools with white students (the South wasn't the only part of the US with segregation, it just had more overt signage). All of which highly incentivized Chinese immigrants to stick together and to stay in places, such as Chinatowns, where they were already established and had some expectation of some degree of safety and predictability.
It was not until the mid 1960s with the rise of the civil rights movement that the Chinese Exclusion Act and anti-Chinese immigrant segregation and "blue laws" were fully struck down. After which significant immigration from China began again. It's only within the last 50 years that Chinatowns in America have even started to "open up" and there has been an opportunity for Chinese-Americans to live elsewhere, pursue whatever jobs they choose, etc. At the same time, there was not a lot of new immigration of Chinese to the US during the period where the Chinese Exclusion Act was law, so many communities in the US became somewhat frozen in a way that other immigrant communities were not. In 1880 there were about 100k Chinese-Americans, in 1940 there were ... about 100k Chinese-Americans. Those Chinatowns in the US were comparatively very stable over that entire period.
Meanwhile, let's look at other ethnic "enclaves". Most of those other enclaves would represent "white" people such as the Irish, Polish, Italians, Germans, Russians, Greeks, etc. Their history is very different from Chinese-Americans. Some of these groups faced oppression but it was nowhere near as severe as what Chinese-Americans faced and it was curtailed many years earlier. For example, Italian, Czech, Portugese, and Greek immigrants (among others) faced severe restrictions from the mid 1920s through to about the 1950s due to the Johnson-Reed Act but through the 1940s and 1950s all of these groups became seen as "white", especially in the backdrop of the growing battle over civil rights.
Which brings us to the Great Migration and the great counter migration (white flight) of the early to mid 20th century. As black Americans moved away from the increasingly intolerable conditions of the South (terrorism, peonage and sharecropping, etc.) and into big cities in the North and on the West Coast they too were hemmed into specific neighborhoods in those destination cities, just as Chinese immigrants were decades earlier. However, whereas there were only about 100k Chinese-Americans in all of the Chinatowns of the US in the early 20th century somewhere around 6 million black citizens would participate in the Great Migration. Even though there were many race riots throughout that period and many attempts to restrict the neighborhoods where blacks were "allowed" to live in cities, fundamentally none of that could stop the growth of black populations in major cities across the country. In response huge numbers of white Americans (which now included even the Italians, the Greeks, the Portugese, etc.) began to flee inner cities (and their erstwhile ethnic enclaves), which further led to "urban decay" as major cities fell in population (and tax revenues) during the mid to late 20th century.
It wasn't until the 1980s and most especially the 1990s that urban centers began to become revitalized as the economy changed from being manufacturing oriented to more knowledge and office worker oriented (and as a new generation saw urban living as more exciting and preferable to the '50s ideal of the sub-urban nuclear family). Populations of most big US cities peak during the mid-20th century (Seattle: 1970, San Francisco: 1950, Detroit: 1950, Baltimore: 1960). Only in the last 20-30 years have those populations started going back up, and only in the last 10-20 years have they surpassed their previous mid century peaks (if they have at all). In other words, the cities of today are almost best seen as brand new cities inhabiting the remains of the cities that were on their way to dying during the mid to late 20th century, and it's been a very comparatively short period of time (only about a single generation) during which those cities have been "alive" in a meaningful sense. Which means that the "frozen in time" Chinatowns from the 1880-1940 period have had only a comparatively short period of time to change much, so they haven't.
Meanwhile, there's also been more of an effort put forward to preserve and celebrate the unique culture of Chinatowns (and Asian-American culture in general) rather than let everything get ground into sameness by the "melting pot" of assimilation.
tl;dr Chinatowns were sort of frozen in time through the late 19th through early 20th century due to extremely hostile anti-Asian sentiment in the US combined with heavily restrictive immigration policies which kept Chinese-American populations nearly static through that time period. Meanwhile, white flight caused the many other ethnic enclaves in major cities to evaporate and meld into larger "white" (instead of German or Polish or Italian or Irish) suburbs. White flight also put substantial pressure on cities by suppressing property values and depriving municipalities of tax funds, all of which led to the stagnation of cities through the mid to late 20th century, a trend which has only turned around within roughly the last generation or so. Which meant that Chinatowns weren't subject to significant development which might have broken them up until fairly recently.
I will add for context that most Australian cities have one or more Chinatowns. Melbourne's is the oldest Chinatown outside of Asia. As in the US, most of these began with large amounts of Chinese immigration related to various gold rushes from the 1850s - 1880s (when the White Australia Policy came in and restricted it).