Donald Trump is the president of the United States but it can sometimes seem that he is not the president of the Golden state.

On a rare two-day visit to California this week, Trump attacked its handling of homelessness and sought to overrule its authority to reduce car emissions.

It was the latest broadside in what has been described as Trump’s long-running “war” with America’s most populous state, where in the 2016 election he gained just 4.48m votes against Hillary Clinton’s 8.75m.

He faces almost certain defeat again next year in this bastion of “the resistance”. But he intends to turn that to his advantage.

“Donald Trump has made a calculation that it’s better be on hostile terms than friendly terms with California because it works as a very convenient political foil for him,” said Bill Whalen, a media consultant for California politicians including the ex-governor Arnold Schwarzenegger. “He was elected by predominantly blue-collar workers in the middle of the country and California is ‘exhibit A’ in their concerns about there the country is headed.”

The president’s trip west raised millions of dollars from rich Republican donors and allowed him to view construction of his long promised border wall. But he also seized the opportunity to exploit the homelessness crisis in big cities.

“We can’t let Los Angeles, San Francisco and numerous other cities destroy themselves by allowing what’s happening,” Trump said, suggesting that it is causing residents of those cities to leave the country. “They can’t believe what’s happening.”

Flying back to Washington on Air Force One, he said the Environmental Protection Agency (EPA) would act against San Francisco, accusing the city of allowing a huge amount of waste, including needles, to go through storm drains into the ocean.

“It’s a terrible situation that’s in Los Angeles and in San Francisco,” he told reporters. “And we’re going to be giving San Francisco – they’re in total violation – we’re going to be giving them a notice very soon.”

Trump had also announced via Twitter that his administration was revoking California’s authority to set CO2 emissions rules that exceed federal standards. He insisted the action would result in safe, less expensive cars and more jobs for Americans but was roundly condemned.

The California congressman Adam Schiff, long a thorn in Trump’s side, tweeted in response: “While Trump launches an all-out assault on the environment, rolling back protections for endangered species, our water, and our air – California remains steadfast. We will not allow our natural resources to be tarnished for generations to come. Not on our watch.”

California was not always hostile territory to Republicans and was home to the former presidents Richard Nixon and Ronald Reagan. But in the current era of political polarisation, it has turned deep blue. In last year’s midterm elections, Democrats gained control of all seven House seats whose districts include Orange county, a conservative stronghold where Nixon was born and raised.

Trump and California have gone toe to toe during his two and a half years in office in politics, in the media and especially in court. The state attorney general, Xavier Becerra, has filed more than 50 lawsuits against the White House over issues including immigration, healthcare and the environment, successfully slowing or blocking some initiatives.

Jessica Levinson, who teaches at Loyola Law School in Los Angeles, told the New York Times in April: “It’s bloody combat. This isn’t a cold war. It’s a scorching hot war. And that’s politically expedient for both sides.”

California is not merely on the defence. Mindful that Trump is the first president for four decades not to make his taxes public, it passed a law that requires candidates for president and governor to release five years’ worth of tax returns to appear on the state’s primary ballot. This received a setback on Thursday when a federal judge ordered a temporary injunction, though officials are likely to appeal.

Culturally, also, although he owns a golf club and other properties in the state, the brash New Yorker finds little comfort in California, where his star on the Hollywood Walk of Fame has been repeatedly vandalised. Trump regularly clashes with actors including Robert De Niro, Debra Messing, Bette Middler, Meryl Streep and Barbara Streisand. He has been denounced from the stage of the Oscars and sought revenge by deriding its TV ratings.

His political foes include California’s Schiff, Senator Kamala Harris, Representative Maxine Waters and the governor, Gavin Newsom, who told HBO’s Axios programme that Republicans were “finished” and likely to become the third party at a national level in 10 to 15 years. He said of Trump: “He has blatantly expressed racist points of view and expressed them on multitudes of occasions during the campaign and since.”

The vice-president, Mike Pence, and other allies join Trump in portraying the state as dominated by liberal elites out of touch with the American heartland. Kevin McCarthy, the House minority leader, insisted that the president was merely highlighting the shortcomings of Democratic leaders who have dominated California for years. “When he’s talking about homelessness and solving that problem, and affordability, that goes across party lines,” he told the Associated Press.

“Drive around – every community, even in Bakersfield – homelessness is the number one issue,” McCarthy said, referring to his own district in the rural Central Valley, just outside Los Angeles.

For Trump, the message is, if you want to see the future under Democrats, look at California Bill Whalen

In July, California became the first state in the country to offer government-subsidised health benefits to young undocumented immigrants, just one example of how the state often leads on issues still being contested by the 2020 Democratic candidates for president.

Whalen, now a research fellow at the Hoover Institution in Palo Alto, said: “Democrats won’t have a debate in California but they are having a debate about California. For Trump, the message is, if you want to see the future under Democrats, look at California.”

This year a man who wore a “Make America great again” hat in a Starbucks in Palo Alto was berated by a woman, who then threatened him on Facebook (she later lost her job). Whalen said: “If you’re a Trump supporter in this part of California, you tread lightly and don’t put a Trump bumper sticker on your car. There’s been a strain of crazy liberalism in California for some time but Trump exacerbated it because of the nature of his victory and because he likes to pick at the wound.”