HONG KONG—In recent years, shares in Chinese companies selling everyday goods like milk, beer and rice crackers have been a safer bet than new-economy businesses such as online platforms and electric-car makers.

That advantage has faded so far in 2022, as surging commodity prices—triggered by the war in Ukraine—threaten to eat into profits of consumer-product makers, and fresh Covid-19 lockdowns in the world’s second-largest economy have dented demand for some goods.

An S&P China consumer-staples index, which includes stocks of companies that produce alcohol, bottled water, dairy products, e-cigarettes and snacks, has tumbled 20% in the year to date, with most of that decline taking place following Russia’s invasion of Ukraine in late February. The sector has underperformed S&P’s broader China index, which has fallen 14% over the same period, and internet giants such as Tencent Holdings Ltd. and Alibaba Group Holding Ltd.

One of the country’s largest brewers, China Resources Beer (Holdings) Co. has fallen 24% this year, while bottled-water and beverage giant Nongfu Spring Co. has dropped 17%. Even longtime market darling Kweichow Moutai Co. , which produces the fiery Chinese spirit baijiu, is down 16%.

Shares of Want Want China Holdings Ltd. , a maker of rice crackers and milk-based drinks, have fared better as people have been stocking up on essentials during the pandemic. Its shares are up slightly in the year to date.

“Many food and beverage companies are being squeezed,” said Min Chen, head of China at Somerset Capital Management in Singapore.

He said investors are more bearish on the outlook for companies that are less able to pass on higher input costs—from ingredients to packaging materials—to the customers that buy their products.

Nongfu Spring, which this week reported a 36% jump in 2021 profit to the equivalent of $1.1 billion, said the Russia-Ukraine war has caused a significant increase in price of the plastic material known as PET, which is tied to international oil prices.

William Yuen, a Hong Kong-based investment director at Invesco, said consumer staples earlier offered a relatively safe haven for investors in Chinese equities during previous market routs that were driven by heavy selloffs in internet-technology and property stocks.

But goods and services related to socializing and entertainment such as beer and other alcohol have also been hit because of China’s latest attempts to stamp out coronavirus outbreaks, he added.

China’s battle with Omicron, which has led to lockdowns and factory closures in multiple cities, further rattled the market in March. In Shanghai, a lockdown planned in two phases started this week—with public transport halted and residents ordered to stay inside their residential compounds.

China has proven to be efficient in carrying out mass testing during lockdowns and the reopening that follows, Mr. Yuen said, citing the relaxing of measures in the southern manufacturing hub of Shenzhen recently after a strict lockdown was imposed. “So when we eventually see that recovery come, it will probably be a lot faster and stronger than what most people anticipated,” he said.

Consumer categories in China can be somewhat arbitrarily defined. A bottle of Moutai can cost the equivalent of hundreds of dollars, giving the namesake liquor giant more discretionary qualities. Moutai in early March said its net profit for the first two months of 2022 jumped 20% from a year ago to the equivalent of $1.6 billion.

Nongfu Spring said the Russia-Ukraine war has caused a significant increase in price of the plastic material known as PET.

Photo: Xing Yun / Costfoto/Zuma Press

Jessica Tea, a senior investment specialist for Asia equities at BNP Paribas Asset Management, said investors should focus on companies offering higher-quality products, since that corporate focus was likely to help those companies grow their market share.

China Resources Beer last week reported flat beer-sales volumes for 2021, but said it is aiming to boost sales of higher-priced products, which could help offset increases in raw materials and packaging costs.

Mr. Chen of Somerset Capital said he remains positive on rising mass consumption in China. He favors companies with products that appeal to younger generations, and that have the power to raise prices. “There’s still a long runway for consumer-facing businesses to grow,” he said.

Write to Elaine Yu at elaine.yu@wsj.com