WASHINGTON (AP) — Don't cross President Donald Trump.

That's the lesson many Republicans are drawing from Rep. Mark Sanford's surprise defeat Tuesday in his primary election in South Carolina. The victor, state Rep. Katie Arrington, repeatedly highlighted Sanford's criticism of the president.

The outcome is a cautionary tale for Republicans in Congress who try to work with Trump while also maintaining their independence. One wrong turn — or in Sanford's case, many — and they could face the wrath of a president who is quick to attack detractors as enemies, even in his own party.

"That's ultimately what the race devolved down to, which was, was I Trump enough?" Sanford told reporters on Capitol Hill.

"It's a very tribal environment right now," he said. "Are you for or against Trump?"

He said he hoped his defeat would not dissuade other members from speaking out against Trump. Agreeing to disagree is "a sign of health in our political system."

Sanford is the second incumbent House Republican to lose a primary this year, though the defeat of Rep. Robert Pittenger in North Carolina came despite his staunch support for the president.

Still, Sanford is only the latest casualty in the intra-party conflict that has roiled the GOP in the Trump era. Trump is known to remember slights from lawmakers.

Rep. Martha Roby, for example, was forced into a runoff last week in Alabama after her opponents seized on her own rift with the president. In 2016, after the release of a tape in which candidate Trump bragged about grabbing women, Roby said she wouldn't vote for him for president.

Recent results have a message, said Rep. Barry Loudermilk in neighboring Georgia. Be a team player or Trump will support someone who will be.

"It doesn't make me nervous, but it probably gives pause to some who want to openly criticize the president," he said.

Trump celebrated Sanford's defeat on Twitter, claiming success in ousting a foe. In a highly unusual move for a president, he had tweeted an endorsement of Arrington on Tuesday afternoon when polls were still open in South Carolina.

"My political representatives didn't want me to get involved in the Mark Sanford primary thinking that Sanford would easily win - but with a few hours left I felt that Katie was such a good candidate, and Sanford was so bad, I had to give it a shot," he said.

The transformation of the GOP under Trump makes some lawmakers uneasy.

It's "becoming a cultish thing, isn't it?" said retiring Sen. Bob Corker of Tennessee, who has an on-off relationship with Trump. "And it's not a good place for any party to end up with a cult-like situation."

To be sure, the president's track record of picking winners and losers in elections is not perfect. He also backed Gov. Henry McMaster, who replaced Nikki Haley in South Carolina, but McMaster was forced into a primary runoff with Greenville businessman John Warren.

And Trump famously backed Roy Moore, the former Alabama judge accused of sexual misconduct with teenage girls, in a special election earlier this year that delivered the state's first Democrat to the U.S. Senate in a generation.

But Trump's preference for populist candidates like Corey Stewart, the Confederate-statue-supporting Republican who won the GOP nomination for Senate in Virginia on Tuesday, increasingly seems to be remaking the GOP, if not Congress, in his image. Stewart will face Sen. Tim Kaine, the Democratic Party's 2016 vice presidential nominee, in the fall.

As former Speaker John Boehner said recently, "There is no Republican Party. There's a Trump Party."

Recent polls have found that more than 80 percent of Republican voters approve of the job Trump is doing, and that makes GOP lawmakers hesitant to criticize him.

"Yes, of course it's his party," said Doug Heye, a former top House GOP leadership aide who is now a party strategist. "That's only more true today, given his high popularity among Republican voters."

He said lawmakers would be wise to keep their differences with the president low-key and within the range of policy, not personality, so as not to alienate Republican primary voters protective of the commander in chief. "What they will not support is someone they view as going after the president personally," Heye said.

Sanford, the former South Carolina governor, had never lost an election before Tuesday, even after a high-profile extramarital affair with a woman in Argentina. But he was never a strong Trump fan.

He demurred during the run-up to the 2016 election when asked if he'd support Trump and spoke out against the president after the election on issues such as limiting Muslim immigrants. He also voted against a border wall proposal, called Trump's proposed tariffs on aluminum and steel "an experiment with stupidity" and was unrelenting in calling for Trump to release his tax returns.

Arrington, the first-term state lawmaker who defeated him on Tuesday, aired ads that featured clips of Sanford speaking critically about Trump's actions.

Still, she didn't initially support Trump's candidacy either. "LOVE MITT ROMNEY!!!!!" Arrington wrote on Facebook in 2016. She later told AP that while she supported Florida Sen. Marco Rubio in the 2016 GOP primary, she eventually backed Trump and "proudly" attended his inauguration

House Speaker Paul Ryan downplayed the rift between Sanford and Trump Wednesday and said there are always going to be winners and losers in the same party during primary season.

"That's just what happens in contested primaries," said the speaker, who is retiring rather than seek re-election.

Others, though, said Sanford's defeat is an up-close example of how Republicans must tread carefully in the Trump era.

Trump ally Rep. Chris Collins of New York offered advice to fellow GOP lawmakers: Say something nice before you bring Trump any complaints.

"I would start by praising the president ... and then say, 'But here's an issue in my local area where I have some disagreement or I'd like to be something different,'" Collins said.

Talking to Trump should be like interactions with your spouse or children when you have a problem that needs airing, he said. Don't just come out immediately "with smash mouth football."

Loudermilk said it's best for lawmakers to take their concerns privately to the White House, as he said he has done behind the scenes on Trump's proposed tariffs. "If it's something I disagree on, it doesn't necessarily mean you have to go out and broadcast it to the world."

___

Kinnard reported from Columbia, South Carolina. Associated Press writers Alan Fram and Kevin Freking contributed from Washington.

___

Follow Mascaro on Twitter at https://twitter.com/LisaMascaro and Kinnard at https://twitter.com/@MegKinnardAP