In these hotly contested spaces political parties surrender to organizations and individuals that maintain order, often through violence. For example, in Mamelodi, a township just outside Pretoria, the Phomolong Residents’ Association acts as the de facto government, collecting fees and resolving disputes. Foreigners and migrants become targets: The association regularly steals from foreign-owned shops to finance its activities.

Local leaders incite hostility to migrants as a means of maintaining their authority. The pattern is widespread. After attacking and clearing out houses occupied by Zimbabweans , Jeff Ramohale, a leader in a township near Pretoria, handed them out to his followers. The informal settlement was named “Jeffsville.”

Similar examples are legion. In Rosettenville, a working-class neighborhood in southern Johannesburg, leaders organized attacks on Nigerians accused of drug use and prostitution. In a Durban township, a business group drove out and kidnapped approximately 50 foreigners. Such vigilantism does little to keep communities safe. Instead it provides the platform for building political careers — and intensifies xenophobia to the point of violence. Behind it all is a failure in government.

It is easy to condemn hatred and denialism. Indeed, African countries and the African Union have rebuked South Africa, threatening it with economic sanctions. Nigerian Afrobeat stars have canceled South African concerts and the Zambian soccer team withdrew in protest from a match. South African embassies and businesses have been attacked, and the ambassador to Nigeria was sternly summoned.

But condemnation is rarely an effective antidote. A campaign led by relatively privileged international and domestic organizations — or even migrants themselves — chastising xenophobic firebrands for their nationalistic sentiments is like pouring gasoline on a fire. After all, what serves their purposes more than being scolded by cosmopolitan elites for trying to protect “national values” and cultures? Such an approach may only harden cultural and political battle lines.

To be effective, interventions must address the incentives for xenophobic violence. This is especially critical in places where migrants and the citizens who live around them suffer the same forms of deprivation — which is the case across Africa and increasingly in neighborhoods in the Middle East, Latin America and the United States.

In South Africa, public awareness campaigns and scoldings are unlikely to work unless there is a serious effort to reshape how the townships are governed. As long as people continue to feel alienated and angry, xenophobic outbursts remain a threat. When the police and formal leaders are distant, unresponsive or part of the problem, people will find alternative solutions. Sometimes these forms of self-government are remarkably amicable and inclusive. Often they are violent. Countering them means stepping into spaces where politicians and the police fear to tread.

South Africa has taught the world many lessons about forgiveness and reconciliation. As violent anti-immigrant rhetoric and policies ripple through Europe, the United States and elsewhere, perhaps it can teach the world another lesson — about how local hatreds emerge, and how they can be stopped.

Loren B. Landau (@lorenlandau) is a professor at the African Centre for Migration and Society at the University of the Witwatersrand and the editor of “Exorcising the Demons Within: Xenophobia, Violence and Statecraft in Contemporary South Africa.”

The Times is committed to publishing a diversity of letters to the editor. We’d like to hear what you think about this or any of our articles. Here are some tips. And here’s our email: letters@nytimes.com.

Follow The New York Times Opinion section on Facebook, Twitter (@NYTopinion) and Instagram.