By Chong Koh Ping, Xie Yu and Joanne Chiu

China's yuan has strengthened to levels last hit in March and local financial markets have rallied as confidence builds that the country's economy is shaking off the coronavirus pandemic.

The optimism has spurred mainland Chinese stocks to multiyear highs in recent days, with the Shanghai Composite rising 16.5% in eight straight sessions through Thursday. Those increases were partly fueled by optimism among China's millions of individual investors, who dominate trading.

There is concern, though, that the gains have been built too rapidly. Chinese state media late this week urged investors to think for the long term. Authorities also highlighted hundreds of operations making illegal loans against shares, indicating that Beijing is eager to prevent stocks from overshooting.

In a sign of such exuberance, QuantumCTek Co. surged roughly 10-fold on its first day of trading in Shanghai on Thursday, giving it a $4.2 billion market value.

Stock-market gains are occurring against a brightening economic backdrop. Some economists expect second-quarter data, due next week, to show China's economy grew in the three months through June. Larry Hu, chief China economist at Macquarie Capital Ltd., forecasts 3% year-over-year growth in gross domestic product, compared with a 6.8% decline in the first quarter.

"It's a V-shaped recovery, but the question remains how resilient it's going to be in the second half," he said. He expects full-year growth of 2%.

The rebound is in contrast to uncertainty that still hangs over economies in Europe and the U.S. A resurgence in coronavirus cases in some states has raised questions about the durability of a reopening of businesses.

While stocks in the U.S. have rebounded from March lows, in particular big technology companies, the Dow Jones Industrial Average and the S&P 500 both remain in the red for the year to date. U.S. stocks closed mixed Thursday; the Dow lost about 1.4% while the tech-heavy Nasdaq Composite Index gained 0.5%.

The rapid run-up in Chinese shares caused a tempering this week of official support for the rally. A front-page editorial Monday in the state-owned China Securities Journal about a "healthy bull market" was viewed as an official endorsement, in a market where investors often take their cues from government action and statements. But on Thursday, the same newspaper urged investors to manage risks, respect the market and invest rationally and for the long term.

"The painful lessons of the abnormal stock market volatility in 2015 is still vivid in our minds, warning us that we must promote a healthy and prosperous stock market in a proper manner," the newspaper said.

Starting in 2014, the Chinese market more than doubled in less than a year, as individual investors borrowed to bet on stocks, with official encouragement. But an epic crash in mid-2015 wiped out most of those gains in slightly over two months.

In another sign of caution, China's securities regulator late on Wednesday listed 258 illegal margin-lending platforms and their operators. These lenders worked through websites, apps and the social-media service WeChat, with some offering to lend investors up to 10 times their capital, it said.

Local investors aren't the only ones jumping into mainland Chinese stocks. Foreign investors bought a net 55 billion yuan ($7.9 billion) of mainland stocks in the first five trading days of this month through the Stock Connect trading link in Hong Kong, Wind data show. That was more than all net purchases last month.

That in part has helped bolster the yuan. On Thursday, China's currency traded below 7 per dollar in the tightly controlled onshore market, as well as in freer offshore markets in Hong Kong and elsewhere.

For years, China prevented the yuan from trading weaker than 7 per dollar, but it let the currency cross that level last summer in the midst of heightened trade tensions, with the central bank saying move wasn't irreversible. Since then the yuan's value has ranged between roughly 6.85 and 7.20 per dollar.

Craig Chan, head of global foreign-exchange strategy at Nomura, said a weaker dollar, China's improving economic outlook and limited recent actions by the Trump administration against China had all helped buoy the currency.

"One thing that tipped the scale was a quite substantial pickup in portfolio inflows into China," Mr. Chan added, referring to increased purchases of Chinese stocks and continued buying of the country's bonds by foreign investors.

A raft of recent survey data has showed economic activity gathering momentum, suggesting Beijing's uncompromising approach to tackling the coronavirus pandemic is starting to pay dividends.

It is also enticing individual investors into stocks. Wang Jian, a 37-year-old project manager at a games company in Shanghai, said there were no better investment opportunities than stocks at the moment. He said the rally can continue, given that investors' concerns about trade conflict and the coronavirus have receded, and valuations are still low by global standards.

"Many people around me are eager to join the party," said Mr. Wang. "Normally they are terrified of investing in the stock market, but now they are eager to put in 10,000 or 20,000 yuan," he said, referring to the equivalent of about $1,430 to $2,860. Mr. Wang said his wife previously opposed his buying more stocks, when the Shanghai Composite was trading at a lower level, but he said she recently started asking if he needed more cash to buy shares.

Tai Hui, chief market strategist for Asia at J.P. Morgan Asset Management, said the wealth effect of surging stocks could encourage more consumer spending, helping to underpin the economic recovery.

Despite the recent run-up, investors and analysts play down concern of a bubble.

While officially sanctioned margin loans have topped 1.3 trillion yuan, the highest in almost five years, that still isn't much more than half the 2015 peak of 2.3 trillion yuan, Wind data show. The Shanghai Composite's valuation, at about 15.4 times historical earnings, is also considerably lower than its 2015 peak.

"The markets are not in a big bubble yet," said Nathan Chow, economist and strategist at DBS Group Research in Hong Kong.

All eyes are on whether China's economic recovery continues to gather steam in the next two quarters to keep pace with the market surge, Mr. Chow said. "If that happens, no problem, everybody is happy," he said. Otherwise, sooner or later, a correction will happen, he said.

--Zhou Wei contributed to this article.

Write to Chong Koh Ping at chong.kohping@wsj.com, Xie Yu at Yu.Xie@wsj.com and Joanne Chiu at joanne.chiu@wsj.com