SHANGHAI — China’s leader, Xi Jinping, has offered carrots to President Trump to stop a trade war that has contributed to a sharp slowdown in the Chinese economy. On Tuesday, he also seemed to brandish a stick.

Kim Jong-un, North Korea’s leader, arrived by train in Beijing on Tuesday to meet with Mr. Xi during the second day of talks nearby between midlevel trade negotiators from China and the United States. Though the government said the events were unconnected, Mr. Kim’s surprise visit was an unmistakable reminder that China could complicate the Trump administration’s pursuit of other goals — including ridding the North of nuclear weapons — if the two powers fail to strike a deal on trade.

The trade talks were originally set to wrap up Tuesday, but wound up being extended into Wednesday. The two sides are laboring to finish a deal before March 2, when higher American tariffs will kick in and escalate the trade war.

Mr. Kim’s arrival was the clearest sign yet that China is looking for ways to prod the United States to settle the dispute quickly. China’s economy has slowed significantly. Both business and consumer confidence have fallen. Car sales have plunged. Weak smartphone purchases contributed to Apple’s warning last week that its sales figures would be lower than expected because of results from China, Hong Kong and Taiwan.