It’s hard to tell whether the animus Mr. Trump has conveyed toward immigrants, particularly Mexicans, is deeply felt, or if he simply came to recognize how powerfully it would appeal to voters disaffected by an uneven economic recovery and the nation’s demographic changes.

But allowing this view to drive trade and foreign policy toward Mexico could have disastrous consequences for workers and consumers in both countries, given how tightly intertwined the two economies have become since the North American Free Trade Agreement went into effect in 1994.

Nafta eliminated most tariffs and other trade barriers among Canada, Mexico and the United States, creating a continent-size market. The agreement led to production chains for cars, planes and other items that straddle borders and provide millions of jobs. Work that requires cheaper labor typically occurs in Mexico, where earnings are lower, while design, engineering and advanced manufacturing tends to take place in Canada and the United States.

Imposing a tariff on Mexico would mean pulling out of Nafta, a move that would severely disrupt the flow of parts and goods across North America and stall production in factories in the United States and Canada. It also could lead to shortages of fresh vegetables and fruits in American grocery stores and drive up the cost of many other consumer goods from Mexico. Mexico’s economy, which is hugely dependent on American trade, would be devastated. But American businesses and workers would stand to suffer immediate harm as well. Mexico would retaliate with tariffs of its own. And no matter how Congress tried to structure the tariff, which would require legislation, it would probably still violate World Trade Organization rules.

Mr. Trump has pointed to America’s trade deficit with Mexico as a sign that the United States is being swindled. Trade with Mexico — imports to the United States totaled $296 billion in 2015 — benefits America by lowering the cost and increasing the availability of goods, like avocados and mangoes in winter. While the trade deficit with Mexico has resulted in job losses in some industries (possibly about 700,000 jobs in the first 16 years), a 2014 study estimates that 1.9 million American jobs depend on exports to Mexico. And trade, by raising wages and the standard of living in Mexico, is a big reason that illegal immigration from Mexico has dropped steadily over the years.