LOS ANGELES — Two years ago, Democrats celebrated a sweep of seven Republican-held congressional seats in California as evidence of the party’s growing ability to compete in swing districts here and across the nation.
But this year, Republicans snatched back four of those seats even as Joseph R. Biden Jr. swamped President Trump in California. The losses stunned Democrats and contributed to the razor-thin margin the party will hold in the House of Representatives this January.
The turnaround is testimony to how competitive the seats are, particularly in Orange County, once a bastion of conservative Republicanism that has been moving steadily Democratic over the past 20 years.
But by any measure, the results were a setback for Democrats in this state and nationally, signaling the steep obstacles they will face in 2022 competing in the predominantly suburban swing districts that fueled their takeover of the House in 2018.
The Democrats’ losses came for a number of reasons, including forces particular to California and the complications of campaigning during a pandemic. But as much as anything, they reflected the potency of Republican attacks, some false or exaggerated, that Democrats were the party of socialism, defunding the police and abolishing private health insurance.
The attacks — led in no small part by Mr. Trump as a central part of his re-election strategy — came at a time when parts of California were swept by street protests against police abuses and racial injustice, some of which turned into glass-shattering bouts of looting and confrontations with law enforcement that were heavily covered on local television.
“Republicans hung around Democrats’ necks that we are all socialist or communist and we all wanted to defund the police,” said Harley Rouda, a Democrat from Orange County who was defeated by Michelle Steel, a Republican member of the Orange County board of supervisors. “In my opinion, we as a party did a less than adequate job in refuting that narrative. We won in 2018 and took the House back because of people like me — moderates — flipping radical Republican seats.”
Republicans said that attempts by Democrats to portray themselves as moderates were undercut by a shift of the party to the left and by the demonstrations.
“It was incredibly easy for us to draw contrasts,” said Jessica Millan Patterson, the leader of the California Republican Party. She said the protests “were happening all over. It looked like a war zone.”
For all that, election outcomes are the result of many factors — and that was particularly the case in a campaign that played out against a deadly pandemic and with such a polarizing figure as Mr. Trump dominating the political debate.
Democrats said they were also hurt by a national policy, set by the party, to avoid door-to-door canvassing during the pandemic. Presumably, that will not be a factor in 2022.
“The No. 1 issue in our campaign is we didn’t canvass,” said Representative T.J. Cox, a Democrat who represents the San Joaquin Valley and lost to David Valadao, the Republican he unseated in 2018. “We didn’t do the door-to-door.” He said it was like playing for a football team that had been told “they can’t pass.”
Christy Smith, a Democrat from northern Los Angeles County who failed to win a district her party captured in 2018, said she had adhered to public health guidelines to her disadvantage. (The Democrat who won in 2018, Katie Hill, stepped down after being accused of having an inappropriate relationship with a staff member.)
“We couldn’t attend a forum or a town hall,” Ms. Smith said. “That’s my favorite way to campaign.”
California’s often lurching effort to combat Covid-19 was damaging in Republican-leaning districts where there has been public defiance of mask-wearing mandates and contempt for the state’s Democratic governor, Gavin Newsom.
“Everyone is concerned about Covid,” said Sam Oh, a Republican consultant to two of the Republican winners, Young Kim and Ms. Steel. “But we are trying to find a path to give small-business owners a way to keep making a life. This is incredibly important and Democrats are tone-deaf to this.”
And Republicans, analysts said, recruited strong candidates, which is always the most critical task in an election. They included Ms. Steel and Ms. Kim, who will be among the first Korean-American members of Congress, and Mike Garcia, a former military pilot who won a special election in May to replace Ms. Hill and then beat Ms. Smith in November.
Democrats set their targets on seven Republican-held seats in 2018 and won all of them, cutting the size of California’s Republican congressional delegation in half. That sweep suggested that Democrats were encroaching on once-Republican areas of the state, presenting a road map for how the party could compete in swing districts across the country.
But Republicans succeeded this time by playing on themes that have long been resonant among moderate voters, particularly in places like Orange County: high taxes, intrusive government and law and order. Democrats said the debates on the national stage hurt them, particularly among Latino and Asian-American voters.
“I think we undervalued the strength of the attack,” said Dan Sena, who was the first Hispanic executive director of the Democratic Congressional Campaign Committee. “The national socialism messaging, combined with the crime messaging, was a death by a thousand cuts in a place like California.”
Ms. Smith said she was frustrated in trying to campaign in an environment “where Republicans are so persistent in false narratives,” and that Democrats had failed to figure out how to address that. “We never got our hands around it,” she said.
For California Republicans, the victories were a rare glimmer of good news for a party that has been in decline in this state. “We now have a blueprint that shows that these really dynamic candidates can win with a presidential turnout, running in a polarized environment,” Mr. Oh said. “We are in an incredibly good position looking forward.”
In one potential sign of a shift, Mr. Newsom is facing a recall campaign, in no small part because of his handling of the pandemic, and while it is unlikely that he will be knocked out of office and replaced by a Republican, it is certainly not impossible. That was how Arnold Schwarzenegger, a Republican, became governor in 2003.
“California’s at a turning point,” Ms. Patterson said. “People are waking up to what Democrats are doing here. This was a referendum on what California Democrats have been doing and what the governor has been doing to this state.”
Republicans argued that the results here — and across the country — were powerful evidence that many voters were rejecting policies that Democrats on the left were advocating.
“Democrats said they were going to pick up 10 to 15 seats in the House,” said Torunn Sinclair, a press secretary for the National Republican Congressional Committee. “Obviously they are missing something. It has to go back to these far-left policies that people just didn’t want. ‘Medicare for all,’ defunding the police.”
The margins of victory in California’s pivot districts were narrow for the Democratic winners in 2018 and for the Republican winners in 2020.
“These districts — including mine — were very difficult to flip,” said Representative Katie Porter, one of the Orange County Democrats who won in 2018 and was re-elected in 2020. “These are Republican districts. They were always going to be very competitive races.”
Darry Sragow, a Democratic strategist, said the state Republican Party had not lost ground in 2020. “But it’s very difficult to argue they are better off,” he said. “Those congressional districts were always Republican up until 2018 when the Democrats picked them off.”
The four losing Democrats are talking about seeking rematches in 2022, though district lines are about to be redrawn as part of the decennial redistricting process that will take place before the next election. And both sides said that the races would be tight again.
“It was a pause,” said Mr. Cox, the San Joaquin Valley Democrat. “But keep in mind we lost by less than 1 percent, without running a field campaign. So it’s not surprising that with factors at play that we came up a little short.”