Thirty-three years after the Food and Drug Administration banned the use of Red Dye No. 3 in red lipstick and other cosmetics by linking it to cancer, California has become the first state to ban the chemical in food.

Gov. Gavin Newsom, a Democrat, signed a law this month that outlawed the red dye and three other chemicals. Red No. 3 is used in Halloween treats and other foods, including private-label candy, cookies and frostings sold at national chains such as Walmart and Target.

The California law increases public pressure on the F.D.A. to examine similar questions surrounding artificial colors and other ingredients. But the agency itself is in a state of flux after the infant formula scandal raised concerns about food oversight. Dr. Robert Califf, the F.D.A. commissioner, has begun a reorganization of its beleaguered food division, marked by the departure of two top officials and the appointment of a deputy commissioner with significant experience in food safety.

Assemblyman Jesse Gabriel, a Democrat who represents a district near Los Angeles, said the federal agency’s inaction along with loopholes in food safety regulations had contributed to the state lawmakers’ reasoning in supporting the measure.

“The primary purpose of this bill was to protect kids and families and consumers in the state of California,” he said. “But a secondary purpose here was to send a message to Washington that the F.D.A. process is broken, and hopefully to spur momentum in Washington D.C. for real, significant change.”

The F.D.A. said in a statement that it “evaluates and regulates ingredients added to food to ensure that the authorized use of these ingredients is safe. This includes the four ingredients included in the California bill.”

The California law applies to food chemicals that are also banned in the European Union and in some other nations. The red dye is still permitted for use in maraschino cherries.

First approved for use in food in 1907, Red Dye No. 3 was banned in cosmetics by U.S. regulators in 1990. At the time, the F.D.A. cited an industry-conducted study that found that the chemical caused thyroid cancer in male rats (but not mice) but estimated that it might cause cancer in fewer than 1 in 100,000 people. Along with prohibiting the dye in cosmetics, the agency pledged to do the same with food.

“We’re stuck in this regulatory quandary where you’re not allowed to apply it to your skin, but you can ingest it in food — so it’s completely illogical,” said Brian Ronholm, director of food policy for Consumer Reports, which signed the petition and supported the California bill.

In the ensuing decades, though, the red dye became a common coloring agent in candies and frosting. Last year, the Center for Science in the Public Interest, along with other advocacy groups, petitioned the F.D.A. to take action against it. The F.D.A. said it is reviewing the petition and has examined the safety of the dye, including the rat studies, which it said are “not relevant to human cancer.”

The petition also cited a California study in 2021 that linked Red Dye No. 3 and other dyes to behavioral problems in some children.

At a grocery store in northern New Jersey, examples could be found of foods that included Red Dye No. 3; Nestle’s strawberry Nesquik drink and the strawberry-flavored Funfetti Mermaid frosting, made by General Mills’s Pillsbury. The synthetic coloring agent is also found in cookies, fruit cocktail cups and sprinkles for treats sold under private labels at Target and Walmart, according to a food database maintained by the U.S. Agriculture Department. Nestle, General Mills, and Walmart did not respond to emails seeking comment. Target said in a statement that it requires its vendors to comply with applicable federal, state and local laws.

Some companies said they were working to remove Red Dye No. 3 from their products. Just Born, which makes Peeps, the squishy marshmallow candy, said only two colors — pink and lavender Peeps — would be made using the dye in 2024. After next spring, the dye would be phased out of all of its products, the company said.

“We have worked hard to develop new formulations,” Keith Domalewski, director of marketing at Just Born, said in an email.

He added that another popular treat, Hot Tamales, was no longer made with Red Dye No. 3 and that updated ingredient lists should soon be on store shelves nationwide.

The California legislation also banned brominated vegetable oil, which is mostly used in store-brand sodas to keep citrus flavoring from floating to the top. An F.D.A. study in 2022 found that the ingredient may disrupt hormones in rats. The agency has said it plans to ban the ingredient.

The state law, which takes effect in 2027, also outlaws potassium bromate, used in baked goods and tortillas. It has been deemed a possible human carcinogen by international experts, also based on animal studies. Propylparaben, also banned under the law, is a preservative in packaged baked goods that is believed to disrupt hormones and affect reproductive health.

The back story of chemicals like propylparaben propelled the bill forward, according to Susan Little, a California-based lobbyist for the Environmental Working Group, an advocacy organization that supported the legislation. The chemical, in F.D.A. parlance, was “generally recognized as safe,” or GRAS, a designation that was initially assigned to ingredients like salt and vinegar, which were deemed permissible foods and chemicals with no in-depth review.

Acknowledging a cumbersome review process, the F.D.A. in 1997 allowed food companies to convene experts internally to certify some ingredients as safe. Companies could add them to food without any special notice to the public or the F.D.A., according to Jensen N. Jose, a lawyer with the Center for Science in the Public Interest.

“Many people were astounded to learn that the F.D.A. is not actively regulating the chemicals we put in our food,” Ms. Little said. “That piece of information came as a surprise to many people and was a reason the bill had the legs it had.”

Opposition to the California legislation was fierce for many months, but diminished when the sponsors dropped the part of the proposal that would have banned titanium dioxide, a chemical used to color food white or add a shiny appearance. The European Union banned the ingredient last year, saying it was uncertain whether the ingredient’s minuscule nanoparticles damaged DNA. The F.D.A. is reviewing a second petition seeking to remove that ingredient from food.

In California, the National Confectioners Association was among the food industry groups opposed to the bill, saying it would generate “a patchwork of inconsistent state requirements.”

“We should be relying on the scientific rigor of the F.D.A. in terms of evaluating the safety of food ingredients and additives,” Christopher Gindlesperger, an association vice president, said.

Frank Yiannas, a former top F.D.A. food official, said after his resignation early this year that he had since consulted for groups that “directly or indirectly have an interest in this topic.” He wrote an opinion article opposing the bill in September. On Thursday, he said that the California law “sets a dangerous precedent” in taking on a food-oversight role better performed by federal experts.

“I don’t think it adequately considered the longer-term, unintended consequences it could have on the U.S. food system,” said Mr. Yiannas, who was a Walmart food safety executive before joining the F.D.A. in 2018.

As the California bill and a pending measure in New York State aimed at eliminating titanium dioxide drew attention, Dr. Califf addressed the F.D.A.’s moves on food safety in May: “The F.D.A. is embarking on a more modernized, systematic reassessment of chemicals with a focus on post-market review.”

He also appealed to Congress for more funding and oversight powers to accomplish those goals. Since then, the F.D.A. has named James Jones to lead its food division, noting his experience as a pesticide regulator at the Environmental Protection Agency.

Nearly 10 years ago, Mars, Kellogg’s and General Mills pledged to remove synthetic colors from popular snacks and cereals, many of which are geared toward children.

But within a couple of years, many of the companies had reversed their decisions. General Mills said sales of its Trix cereal, appearing in more muted colors thanks to vegetable and fruit juices and other ingredients, initially exceeded expectations. But the company soon backpedaled, saying loyal customers were split, with some preferring brightly colored cereal made with artificial colors. WK Kellogg, a spinoff of Kellogg’s cereal business, did not respond to queries seeking comment.

In a statement, Mars said after research it had found that, “consumer expectations regarding colors in food differ widely across markets and categories.”

In grocery store aisles, a vast majority of red-hued beverages and treats, including Skittles, cherry Pop-Tarts, Fruity Pebbles cereal and strawberry Pedialyte, are made with Red Dye No. 40, another synthetic coloring.

The dye, also known as Allura Red, has also drawn scrutiny over the years. Some studies and researchers have linked the dye to hyperactivity in children, but a committee convened by the U.S. Department of Agriculture in 2011 found no “causal link.” Late last year, another study in mice raised concerns that Allura Red dye could increase the risk of inflammatory bowel disease.

Supporters of the California measure said they hoped the F.D.A. and others would focus more on such additives and on overall food chemical oversight in the coming months.

“This regulatory system couldn’t get any better for the food industry and companies that sell chemicals to the food industry,” said Scott Faber, director of the Environmental Working Group.