A restaurant in northern China put up a banner celebrating the virus’s spread in the United States. A widely circulated cartoon showed foreigners being sorted into trash bins. African residents in the southern city of Guangzhou, including Mr. Mwamba, have been corralled into forced quarantines, labeled as dangers to the country’s health.

Some of the uglier manifestations of nationalism have been fueled by government propaganda, which has touted China’s response to the virus as evidence of the ruling Communist Party’s superiority. And recriminations from abroad, including calls to make China pay for the pandemic that began there, have triggered defensiveness on the part of many Chinese.

Whipping up national pride has long been a tool for solidifying the party’s grip on power. In the short term, the nationalism may be useful to the central government, as it seeks to quell lingering discontent over its early attempts to play down the outbreak.

But if left unchecked, the vitriol risks isolating China internationally, just as the Communist Party seeks to use the pandemic to promote itself as a global leader.